不同的纵向和横向自动布局约束:当设备首先处于横向时在哪里设置它们?

Different portrait and landscape autolayout constraints: where to set them when the device is firstly in landscape?

我已将 NSLayoutConstraint 设置为 IBOutlet,我在 storyboardIB 中设置的值用于纵向。当我 运行 纵向应用程序然后将设备旋转为横向时,我管理约束更新。但是当我 运行 应用程序是已经处于横向状态的设备时,约束具有纵向值。

加载视图控制器并显示视图时,我应该在哪里检查设备的当前方向,并根据该方向设置适当的约束?

谢谢

您最初可以在 viewDidLoad 方法中保留一个条件

if(UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])){
    // Apply Portrait Constraints
}
else{
    // Apply Landscape Constraints
}

稍后当用户改变方向时,您可以检查下面的方法并应用相同的条件

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
          // Apply Portrait Constraints
    }
    else{
          // Apply Landscape Constraints
    }
}