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

标签 ios swift uiview autolayout

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

相关文章:

ios - 如何在 superview 中定位 UIView?

ios - 如何使用像素化更正 UIView 和 UILabel 文本缩放?

ios - Swift - 如何将 View Controller 的引用传递给子 UIView 类?

iphone - 我可以使用 mailto : as support URL in iTunes Connect

ios - UICollectionView 是否滚动到索引?

ios - 如何将 NSString 散列为 SHA512

swift - a 和 b 之和减去 c 是多少?

按下主页按钮时 iOS 应用程序崩溃(当应用程序暂停时)

Swift 过滤嵌套的对象数组

ios - Swift - 如何根据整数中的索引位置从 UICollectionView 获取 UICollectionViewCell