简体   繁体   中英

Programmatically Auto-layout

I created a UIView called "holderView" and the issue for me is that it is only appearing in the spot i want it to for iPhone 6, since that is what i created and tested it with. I tested it on other devices and the "holderView" is in different spots. Here is the code:

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])

I believe the problem is in the holderViewCenterXConstraint and holderViewCenterYConstraint, where i change the constants to values to put the "holderView" in the spot i wanted on iPhone 6.

How can i get this holderView in the correct spot across all screen sizes? If the problem is the constants, how should i change it?

While equally width and height put constant at 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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