繁体   English   中英

以编程方式自动布局

[英]Programmatically Auto-layout

我创建了一个名为“ 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? 如果问题是常量,我应该如何更改?

宽度和高度相等时,常数zero

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM