【问题标题】:App support the app Landscape and portrait mode in both ipad and iphone应用程序支持ipad和iphone中的应用程序横向和纵向模式
【发布时间】:2014-03-12 07:24:44
【问题描述】:

在我的应用程序中,我尝试创建一个界面构建器,支持 ipad 和 iPhone 中的横向和纵向。

[在Android中我们使用fill parent来自动调整动态创建的UI-Elements。iOS中是否有任何语法来自动调整大小]

  1. 如何动态创建UI元素同时支持横向模式和纵向模式?

  2. 如何创建视图控制器来支持横向模式和纵向模式?

  3. 是否需要动态创建所有视图和 UI 元素?

【问题讨论】:

标签: ios iphone objective-c ipad


【解决方案1】:

1)如果您将制作 xib 或 nib,而不是仅以一种模式(纵向或横向)开发 xib 或 nib。比对任何控件使用如下图像的自动调整大小选项。

http://www.raywenderlich.com/50319/beginning-auto-layout-tutorial-in-ios-7-part-2。您可以使用此链接进行自动布局。

但自动布局无法按您的意愿正常工作。因此,即使您使用自动布局,您也需要以编程方式设置控制框架。

2) 如果你想动态开发而不是使用下面的代码,你可以设置所有控件的框架。 在ViewwillAppear中。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:nil];

viewdidload 如下设置您的控件。

UILabel lbl - [UILabel alloc]init];


-(void)orientationChanged{

       if(Orientation is portrait){
            [lbl setFrame:for portrait];
    }else{
           [lbl setFrame: for landscape];
    }

如果设备更改模式比上述通知触发并在该方法中。您可以设置控制框架。

希望你能得到答案。

【讨论】:

  • 我必须在orientationChanged方法中实现什么
  • 您必须更改控件框架。如果纵向比设置它,如果横向比设置它。明白了吗?
  • 我的问题是如何动态创建 UI 元素以支持横向和纵向模式
【解决方案2】:

最可靠的方法 - 在视图控制器中创建一个方法 -

-(void)setFrameForOrientationChange:(UIDeviceOrientation*) o {
//... 
 implement your code for frame settings..
}

【讨论】:

    【解决方案3】:

    您可以使用自动布局来提供纵向和横向模式。

    更多详情,请查看:What is Auto Layout?

    您必须设置横向和纵向模式的约束才能工作。就像如果你想在顶部有一个按钮,你可以在它上面设置约束:从上到左等等。

    如果您希望 UI 元素能够动态更改,您只需根据需要更改框架方向。示例代码在这里:

    # pragma mark - Orientation related methods
    
    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration NS_AVAILABLE_IOS(3_0)
    {
        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            [self deviceRotatedToLandscapeMode];
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            [self deviceRotatedToLandscapeMode];
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
            [self deviceRotatedToPortraitMode];
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            [self deviceRotatedToPortraitMode];
        }
    
    }
    
    - (void) deviceRotatedToPortraitMode {
    
        self.mTableView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height);
    
    }
    
    - (void) deviceRotatedToLandscapeMode {
    
        self.mTableView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.height, self.view.frame.size.height);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多