在 IB 中,无法在标签的对齐属性上添加变体。我的需要是在宽度紧凑时将文本对齐在左侧,而在宽度正常时将文本居中。
我能怎么做?
谢谢
您可以将此行为添加到
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
super.willTransition(to: newCollection, with: coordinator)
switch newCollection.verticalSizeClass {
case .compact:
yourLabel.textAligment = UITextAligment.left
case .regular, .unspecified:
yourLabel.textAligment = UITextAligment.center
}
}
确定旋转使用verticalSizeClass,确定设备类型(如iPad 或iPhone)使用horizontalSizeClass。
if(width >= yourDesiredWidth){
yourLabel.textAligment = UITextAligment.center
}
else{
yourLabel.textAligment = UITextAligment.left
}