2017-08-08 57 views

回答

1

可以在

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)使用horizo​​ntalSizeClass。

+0

好的,所以IB中无法做到这一点。谢谢。 – Julien

+0

不幸的是,与创建代码相比,IB创建和操作UI元素的可能性更小。 –

0
if(width >= yourDesiredWidth){ 
    yourLabel.textAligment = UITextAligment.center 
} 
else{ 
    yourLabel.textAligment = UITextAligment.left 
}