gpt4 book ai didi

swift - 不同设备上的 NSLayoutConstraint 问题

转载 作者:行者123 更新时间:2023-11-28 06:47:50 26 4
gpt4 key购买 nike

我正在使用 NSLayoutconstraint,但从 iphone 6 plus 传递到 iphone 6 时,它似乎弄乱了 View 的位置。

它应该可以工作,因为我正在创建与 View 属性相关的约束,在使用不同设备的情况下应该更改该属性。

这里是约束的一些例子:

v=getBitmapView("V -.-")
self.view.addSubview(v)
var myConstraint =
NSLayoutConstraint(item: v,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.CenterX,
multiplier: 1.0,
constant: 0)



self.view.addConstraint(myConstraint)
myConstraint =
NSLayoutConstraint(item: v,
attribute: NSLayoutAttribute.BottomMargin,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.BottomMargin,
multiplier: 1.0,
constant: -30)



self.view.addConstraint(myConstraint)
myConstraint =
NSLayoutConstraint(item: v,
attribute: NSLayoutAttribute.Width,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.Width,
multiplier: 1.0,
constant: -200)



self.view.addConstraint(myConstraint)
myConstraint =
NSLayoutConstraint(item: v,
attribute: NSLayoutAttribute.Height,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.Height,
multiplier: 1.0,
constant: -650)



self.view.addConstraint(myConstraint)

如果我运行 iphone 6 plus, View 位于底部边距上方 30 点,但如果我使用 iphone 6, View 就会消失。

最佳答案

问题是您对高度和宽度进行了硬编码。与其使用宽度和高度常量,不如将它们定义为父 View 的某个其他 View 的倍数。

How can I set the width and height without hard-coding? Do you have an example? Let's say i want to have a view 1/3 of the half of the screen that is 1/6 of the screen

使用乘数来完成此操作。如果只有一个 subview ,则可以将宽度和高度定义为父 View 的倍数:

NSLayoutConstraint.activateConstraints([
blueView.topAnchor.constraintEqualToAnchor(view.topAnchor), // pin to top left corner
blueView.leftAnchor.constraintEqualToAnchor(view.leftAnchor),

blueView.widthAnchor.constraintEqualToAnchor(view.widthAnchor, multiplier: 1.0 / 3.0), // one third the width
blueView.heightAnchor.constraintEqualToAnchor(view.heightAnchor, multiplier: 1.0 / 2.0) // one half the height
])

产生:

enter image description here

或者,有时,您定义了一堆 View 来跨越整个父 View ,然后定义它们相对于彼此的宽度,例如,上半部分是三分之一蓝色和三分之二红色,下半部分全是绿色:

NSLayoutConstraint.activateConstraints([
blueView.topAnchor.constraintEqualToAnchor(view.topAnchor), // pin blue and red to the top
redView.topAnchor.constraintEqualToAnchor(view.topAnchor),

blueView.leftAnchor.constraintEqualToAnchor(view.leftAnchor), // pin blue to left edge
redView.leftAnchor.constraintEqualToAnchor(blueView.rightAnchor), // pin red adjacent to blue view
redView.rightAnchor.constraintEqualToAnchor(view.rightAnchor), // pin red to right edge
redView.widthAnchor.constraintEqualToAnchor(blueView.widthAnchor, multiplier: 2.0), // but make red twice as wide as blue (i.e. 2/3rds entire view)

greenView.topAnchor.constraintEqualToAnchor(blueView.bottomAnchor), // define green to be below blue
greenView.topAnchor.constraintEqualToAnchor(redView.bottomAnchor), // and red views

greenView.heightAnchor.constraintEqualToAnchor(blueView.heightAnchor), // but make it same height as blue

greenView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor), // and pin it to the bottom of superview

greenView.leftAnchor.constraintEqualToAnchor(view.leftAnchor), // and green spans entire superview
greenView.rightAnchor.constraintEqualToAnchor(view.rightAnchor)
])

enter image description here

显然,如果您更愿意实例化一个 NSLayoutConstraint 然后执行 addConstraint,就像您在原始示例中所做的那样,那么请放心。上面的语法(在 iOS 9 中引入)只是一种更简洁的演绎,但概念与上面相同,即应该根据需要固定边缘,但只将宽度/高度定义为另一个 View 的倍数,而不是使用一些硬编码常量。

关于swift - 不同设备上的 NSLayoutConstraint 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35807954/

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