我可以在横向模式和纵向模式下使用界面构建器对我的字段的不同位置吗?



我可以在横向模式和纵向模式下使用界面生成器对我的字段的不同位置吗?(完全不同,所以我不能只使用布局属性)?

还是代码是唯一的出路?

谢谢

您可以使用

willRotateToInterfaceOrientation方法。当您更改设备方向时,它将调用。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
  return YES;
}
-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
 {
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) 
    {
      [label setFrame:CGRectMake(20, 52, 728, 617)];
    }
     else
    {
    [label setFrame:CGRectMake(20, 52, 728, 617)];
    }
 }

我会说,如果纵向和横向模式共享的字段相同,请执行代码。如果每种模式中都有不同的对象,那将不是一个好主意。

您可以在界面构建器中保留两个UIViews,当用户旋转设备时,您可以隐藏一个并根据方向显示另一个UIViews。你能试试下面的代码行吗?

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    if(([self.navigationController.visibleViewController interfaceOrientation] == UIDeviceOrientationLandscapeLeft) || ([self.navigationController.visibleViewController interfaceOrientation] == UIDeviceOrientationLandscapeRight)){
        self.viewLandscape.hidden = NO;
        self.viewPortrait.hidden = YES;
    }
    else {
        self.viewLandscape.hidden = YES;
        self.viewPortrait.hidden = NO;
    }
}

以下是您可以使用的方法。最好的方法是在顶部
1.最好使用自动布局来调整视图。
2.自动布局+代码
3. 仅代码。
4.您可以为xib制作两个视图,一个用于横向视图,一个用于纵向视图。并根据方向显示和隐藏。但在这种情况下,您需要将所有纵向视图与横向视图(文本等属性)同步,反之亦然。这很容易维护,但您必须为同步每个视图的属性而付出额外的麻烦。

最新更新