为什么我的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)
}
答案 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设置相对位置。如果你想让三个圆圈相互保持相同的相对位置,你也应该在它们之间设置约束。