【问题标题】:How to change view of the viewcontroller when change the orientation in iPhone?更改 iPhone 中的方向时如何更改视图控制器的视图?
【发布时间】:2011-07-05 08:49:05
【问题描述】:

我正在开发一个支持 2 个方向的 iPhone 应用程序。我有这个视图控制器的 2 个 UIView 文件。我需要根据设备界面方向给视图控制器设置对应的UIView文件。你能指导我如何改变方向吗?

【问题讨论】:

    标签: iphone orientation


    【解决方案1】:

    为什么要为两个方向使用两个视图?您可以设置控件的 autoresizemask 属性,如果纵向和横向两种模式具有相似的控件以显示在同一位置。否则,您需要在

    中使用两个视图
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
    

    或者你可以得到 UIDevice 的通知

     UIKIT_EXTERN NSString *const UIDeviceOrientationDidChangeNotification;
    
    - (void)beginGeneratingDeviceOrientationNotifications;      // nestable
    - (void)endGeneratingDeviceOrientationNotifications;
    

    【讨论】:

      【解决方案2】:

      假设您已将-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 设置为返回您支持的方向,您可以使用以下方法,这些方法都可以在您的 UIViewController 中设置来检查方向变化,然后设置您的视图:

      // called just before the user interface starts to rotate - my advice would be to use this method to save a copy of the current orientation.
      -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
      
      // called after the interface has finished rotating. Here, you can compare the old orientation with the new one - if you went from portrait to landscape, then update your views accordingly.
      -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
      

      希望这会有所帮助!

      【讨论】:

        【解决方案3】:

        只需在你的控制器中为这两个方向提供这个委托。

        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
            return YES;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多