2
我有開發一種DeviceLayout
庫enter link description hereIBDesignable渲染NSLayoutConstraint子類
這個庫提供DeviceLayoutConstraint
設置每個設備的大小自動版式恆定
import UIKit
import Device
class DeviceLayoutConstraint: NSLayoutConstraint {
@IBInspectable var inch3_5: CGFloat = 0.0 { didSet { updateConstant(size: .screen3_5Inch, constant: inch3_5)}}
@IBInspectable var inch4: CGFloat = 0.0 { didSet { updateConstant(size: .screen4Inch, constant: inch4)}}
@IBInspectable var inch4_7: CGFloat = 0.0 { didSet { updateConstant(size: .screen4_7Inch, constant: inch4_7)}}
@IBInspectable var inch5_5: CGFloat = 0.0 { didSet { updateConstant(size: .screen5_5Inch, constant: inch5_5)}}
@IBInspectable var inch7_9: CGFloat = 0.0 { didSet { updateConstant(size: .screen7_9Inch, constant: inch7_9)}}
@IBInspectable var inch9_7: CGFloat = 0.0 { didSet { updateConstant(size: .screen9_7Inch, constant: inch9_7)}}
@IBInspectable var inch12_9: CGFloat = 0.0 { didSet { updateConstant(size: .screen12_9Inch, constant: inch12_9)}}
fileprivate func updateConstant(size: Size, constant: CGFloat) {
if size == deviceSize() {
self.constant = constant
layoutIfNeeded()
}
}
open func deviceSize() -> Size {
return Device.deviceSize
}
open func layoutIfNeeded() {
self.firstItem.layoutIfNeeded()
self.secondItem?.layoutIfNeeded()
}
}
fileprivate extension Device {
static let deviceSize = Device.size()
}
每設備在運行時模擬器施加恆定屬性或設備
我的曲注意是
選擇設備時是否可以使用IBInspectable或IBDesignable更新渲染?
我想在故事板或XIB文件,檢查渲染當設備類型改變
完全不相關,但我不確定我會建議在'updateConstraint'中調用'layoutIfNeeded'。我可能會調用'setNeedsLayout'。如果您碰巧正在更新一堆常量,則不希望觸發太多冗餘佈局週期。 – Rob