gpt4 book ai didi

swift - 如何在 swift 中使用 NSLayoutConstraint 根据屏幕尺寸更改设计

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

我为 iPhone XR 使用 Storyboard约束设计了一个屏幕,但该设计在 iPhone7 中变大了,所以我想使用 NSLayoutConstraints 更改我的 ContainerView 的高度和宽度>.

我采取了:

@IBOutlet weak var containerViewheight: NSLayoutConstraint!

这个ContainerView在iPhoneXR中实际高度是300,所以我想在iPhone7中改变它

例如:

if (iphone7) {
self.containerViewheight.constant = 240
}

if (iphone SE) {
self.containerViewheight.constant = 280
}

不幸的是,我对尺寸等级知之甚少。

最佳答案

你总是可以向 UIScreen 类方法 main 询问当前的具体大小,它返回当前设备的具体大小。

let deviceSize = UIScreen.main.bounds.size

但您可能知道,heightwidth 会根据设备的方向(横向或纵向)发生变化。

也许这个扩展可以帮助:

import UIKit

extension UIScreen {

/// Retrieve the (small) width from portrait mode
static var portraitWidth : CGFloat { return min(UIScreen.main.bounds.width, UIScreen.main.bounds.size.height) }

/// Retrieve the (big) height from portrait mode
static var portraitHeight : CGFloat { return max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height) }

/// Retrieve the (big) width from landscape mode
static var landscapeWidth : CGFloat { return max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height) }

/// Retrieve the (small) height from landscape mode
static var landscapeHeight : CGFloat { return min(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height) }
}

关于swift - 如何在 swift 中使用 NSLayoutConstraint 根据屏幕尺寸更改设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58320775/

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