[英]iOS interface builder auto constraints problem
所以,這似乎是你所追求的。 這是在 6s 上:
這是在 iPhone 11 上的:
這是在 iPhone 6s 上旋轉的很好的衡量標准:
如您所見,有四個“組”——兩個標簽、標簽和文本字段、第二個標簽和文本字段以及按鈕。 它們在兩個屏幕上從上到下均勻分布。
這是正確的想法,不是嗎?
那是怎么做的呢? 簡單的。 一個垂直堆棧視圖填充屏幕,分布設置為等間距。 在里面,一個 UIView 包含每個組(除了它自己的按鈕),每個 UIView 都由它自己的高度約束給出一個固定的高度。 還有一點,但這是問題的核心。 一旦你有了它,你當然可以根據需要進一步調整。
絕對沒有代碼; 整個事情都在 Xcode 的筆尖編輯器(“界面構建器”)中配置了一會兒。
聽起來您正在嘗試根據設備的大小在輸入之間創建間距? 我不知道它是否會像您期望的那樣,但我之前在不同的情況下都必須這樣做。 您可以使用類似NSLayoutConstraint
子類的內容來滿足您的需求。 本質上,根據約束是垂直的還是水平的,這個類會在運行時計算屏幕尺寸,並根據你給它的百分比值修改約束尺寸。
/// Layout constraint to calculate size based on multiplier.
class PercentLayoutConstraint: NSLayoutConstraint {
@IBInspectable var marginPercent: CGFloat = 0
var screenSize: (width: CGFloat, height: CGFloat) {
return (UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
}
override func awakeFromNib() {
super.awakeFromNib()
guard marginPercent > 0 else { return }
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(layoutDidChange),
name: UIDeviceOrientationDidChangeNotification,
object: nil)
}
/**
Re-calculate constant based on orientation and percentage.
*/
func layoutDidChange() {
guard marginPercent > 0 else { return }
switch firstAttribute {
case .Top, .TopMargin, .Bottom, .BottomMargin:
constant = screenSize.height * marginPercent
case .Leading, .LeadingMargin, .Trailing, .TrailingMargin:
constant = screenSize.width * marginPercent
default: break
}
}
deinit {
guard marginPercent > 0 else { return }
NSNotificationCenter.defaultCenter().removeObserver(self)
}
}
來源: https : //basememara.com/percentage-based-margin-using-autolayout-storyboard/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.