为什么我的UIView在不同的iPhone上有不同的位置?

时间:2016-04-25 16:44:19

标签: swift autolayout autoresizingmask

为什么我的UIView在不同的iPhone屏幕尺寸上有不同的位置?我想这是因为autoresizingMask但是在应用autoresizingMask之后我怎样才能获得一个视图框架来创建一些通用逻辑?或者最新的做法是什么?

这是代码:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

看起来您只使用self.view设置相对位置。如果你想让三个圆圈相互保持相同的相对位置,你也应该在它们之间设置约束。