gpt4 book ai didi

ios - Swift - 根据屏幕大小设置 UIView 的大小

转载 作者:行者123 更新时间:2023-11-29 01:07:23 32 4
gpt4 key购买 nike

我在这里浏览了很多线程并尝试了两个最推荐的解决方案。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/

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