以编程方式自动布局
问题描述:
我创建了一个名为“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)
所以,如果我把用于的centerX和centerY常数为0,则holderView将在屏幕的中心,但我想保持器视图稍微低于右上角,而不是那么远。它是一个特定的地方。我怎样才能修改holderView的位置,并在同一位置为所有屏幕尺寸。 谢谢你的回应! –