2016-04-25 40 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 
相关问题