以编程方式自动布局

时间:2015-12-07 05:12:09

标签: ios swift autolayout

我创建了一个名为“holderView”的UIView,对我来说问题是它只出现在我希望它用于iPhone 6的地方,因为这是我创建并测试它的。我在其他设备上进行了测试,“holderView”位于不同的位置。这是代码:

var holderView = UIView()
let holderWidth = defaultTimeCapImage?.size.width
let holderHeight = (defaultTimeCapImage?.size.height)! + (vineTimeCapImage?.size.height)! + (instagramTimeCapImage?.size.height)! + 40

    self.holderView.hidden = false
    self.holderView.translatesAutoresizingMaskIntoConstraints = false
    self.addSubview(self.holderView)



    let holderViewCenterXConstraint = NSLayoutConstraint(item: self.holderView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 155)

    let holderViewCenterYConstraint = NSLayoutConstraint(item: self.holderView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -205)

    let holderViewWidthConstraint = NSLayoutConstraint(item: self.holderView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant:holderWidth!)

     let holderViewHeightConstraint = NSLayoutConstraint(item: self.holderView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant:holderHeight)

    self.addConstraints([holderViewCenterXConstraint,holderViewCenterYConstraint, holderViewWidthConstraint,holderViewHeightConstraint])

我认为问题出现在holderViewCenterXConstraint和holderViewCenterYConstraint中,我将常量更改为值,将“holderView”放在iPhone 6上我想要的位置。

如何在所有屏幕尺寸的正确位置获得此holderView? 如果问题是常数,我该如何改变?

1 个答案:

答案 0 :(得分:0)

同样宽度和高度保持恒定在netstat -an|grep 8000

zero