gpt4 book ai didi

ios - 以编程方式自动布局

转载 作者:行者123 更新时间:2023-11-30 13:52:11 25 4
gpt4 key购买 nike

我创建了一个名为“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)

关于ios - 以编程方式自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34126881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com