我在这里浏览了很多线程并尝试了两个最推荐的解决方案。 ViewDidLoad() 方法内部:
self.darkBackgroundWithButtons.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height * 0.254)
或
self.darkBackgroundWithButtons.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height * 0.254)
此外,在我的 Storyboard中,我将 View 的高度约束设置为低优先级 (如果我不在 Storyboard中设置高度,xcode 会提示布局不明确)
但是上面的这些代码行都没有对 darkBackgroundWithButtons 做任何事情,它对于每个设备尺寸都保持相同的高度
最佳答案
这可能是问题所在:
在界面生成器中,您为按钮设置了约束,因此当您尝试更新框架时它不会改变高度。改为这样做:
首先将您的约束从界面构建器连接到您的 View Controller ,就像您通常使用按钮那样做。
然后使用此代码更改约束:
var index = self.darkBackgroundWihButtons.constraints.indexOf("your constrain name")!
self.darkBackgroundWithButtons.constraints[index].constant = 0.2 // or whatever number you want
关于ios - Swift - 根据屏幕大小设置 UIView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36183951/