2016-04-25 61 views
0

爲什麼我的UIView在不同的iPhone屏幕尺寸上有不同的位置?我想這是因爲autoresizingMask,但我應用autoresizingMask後如何獲得一個視圖框架,以創建一些通用邏輯?或什麼是處理這個最好的做法?爲什麼我的UIView在不同的iPhone上有不同的位置?

這裏是代碼:

private func setup() { 

     positionView = CircularProjectPositionControl(frame: self.bounds) 
     positionView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] 
     positionView.outerCircleWidth = outerCircleWidth 

     assetsView = CircularProjectAssetsControl(frame: self.bounds) 
     assetsView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] 
     assetsView.backgroundColor = UIColor.clearColor() 
     assetsView.imageWidth = outerCircleWidth 

     let size = CGSize(width:40, height:40) 
     let width = floor(CGRectGetMidX(self.bounds) - size.width/2.0) 
     let height = floor(CGRectGetMidY(self.bounds) - size.width/2.0) 
     collaboratorView = AvatarView(frame: CGRect(x: width, y: height, width: size.width, height: size.height)) 
     collaboratorView.autoresizingMask = [.FlexibleBottomMargin, .FlexibleLeftMargin, .FlexibleRightMargin] 

     self.addSubview(assetsView) 
     self.addSubview(positionView) 
     self.addSubview(collaboratorView) 
    } 

iPhone 6S: enter image description here

iPhone 5: enter image description here

+0

你爲什麼編程這樣做呢?在Storyboard –

+0

@ matt.writes.code中使用自動佈局約束會容易得多。我不能在這裏使用storyboard –

回答

0

貌似只設置了與self.view的相對位置。如果你想讓三個圓圈有一些相互保持相同的相對位置,你還應該在它們之間設置約束。

+0

所以我應該添加一個自動佈局約束或autoresizingMask掩碼? –

1

嘗試增加.CenterX & .CenterY約束上居中每個子視圖:

NSLayoutConstraint(item: view1, attribute: .CenterX, relatedBy: .Equal, toItem: view2, attribute: .CenterX, multiplier: 1, constant: 0).active = true 
NSLayoutConstraint(item: view1, attribute: .CenterY, relatedBy: .Equal, toItem: view2, attribute: .CenterY, multiplier: 1, constant: 0).active = true 
相關問題