【问题标题】:iPhone View Controller landscape / portrait rotation troublesiPhone View Controller 横向/纵向旋转的烦恼
【发布时间】:2014-01-20 18:17:04
【问题描述】:

我正在尝试为我的应用程序的不同子模式实施强制纵向/横向方向。为此,我有一个 UINavigationController 作为根控制器,并且每个子模式都有自己的视图控制器,它们是其中之一

@interface iosPortraitViewController : UIViewController

或

@interface iosLandscapeViewController : UIViewController

与

-(BOOL)shouldAutorotate;
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
-(NSUInteger) supportedInterfaceOrientations;

重载并根据每个方向类型正确设置。例如iosLandscapeViewController::supportedInterfaceOrientations 返回 UIInterfaceOrientationMaskLandscape。

当应用程序中的子模式发生变化时,相应的视图控制器会使用 present/dismissViewController 呈现在根视图控制器上,这会强制方向重新评估并调用重载视图控制器类中的函数并相应地定位自身.

我的问题是当我们切换到横向时,子模式视图的框架从屏幕的左上角偏移,这是它应该在的位置(这是一个显示背景图片的全屏视图)。

出于调试目的,如果我将该子模式的视图控制器更改为 iosPortraitViewController,则视图信息为:

size = 480.000000 320.000000
bounds = 0.000000 0.000000 480.000000 320.000000
frame = 0.000000 0.000000 480.000000 320.000000
centre = 240.000000 160.000000
user interaction enabled = 1
hidden = 0
transform = 1.000000 0.000000 0.000000 1.000000 : 0.000000 0.000000

在横向模式下,这是它所需要的,视图信息是:

size = 480.000000 320.000000
bounds = 0.000000 0.000000 480.000000 320.000000
frame = 80.000000 -80.000000 320.000000 480.000000 
centre = 240.000000 160.000000
user interaction enabled = 1
hidden = 0
transform = 0.000000 -1.000000 1.000000 0.000000 : 0.000000 0.000000 

框架的 80、-80 原点是我遇到的问题 - 它应该是 0,0。 (如果有人能指出它是如何获得 80,-80 的,那将不胜感激 - 我可以看到 X 而不是 Y)。

还要注意框架中的 w 和 h 是如何交换的,并且变换是旋转变换 - 从阅读中,我猜 UIWindow(它始终处于纵向模式)已将此应用于视图变换根视图控制器?

我能做些什么来解决这个问题?我需要视图控制器视图的框架位于正确的位置(即原点位于 0,0)。我尝试对其进行硬编码,但它似乎不起作用,而且它也不是一个很好的解决方案 - 我更了解发生了什么以及如何正确修复它。

谢谢!

:-)

【问题讨论】:

    标签: ios iphone objective-c uiview uiviewcontroller


    【解决方案1】:

    要支持备用横向界面,您必须执行以下操作:

    1. 实现两个视图控制器对象。一个呈现纯纵向界面,另一个呈现纯横向界面。
    2. 注册 UIDeviceOrientationDidChangeNotification 通知。在您的处理程序方法中,根据当前设备方向显示或关闭备用视图控制器。

    从苹果指南到Creating an Alternate Landscape Interface

    也来自指南:

    @implementation PortraitViewController
    - (void)awakeFromNib
    {
        isShowingLandscapeView = NO;
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(orientationChanged:)
                                     name:UIDeviceOrientationDidChangeNotification
                                     object:nil];
    }
    
    - (void)orientationChanged:(NSNotification *)notification
    {
        UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
        if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
            !isShowingLandscapeView)
        {
            [self performSegueWithIdentifier:@"DisplayAlternateView" sender:self];
            isShowingLandscapeView = YES;
        }
        else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
                 isShowingLandscapeView)
        {
            [self dismissViewControllerAnimated:YES completion:nil];
            isShowingLandscapeView = NO;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多