代码之家  ›  专栏  ›  技术社区  ›  Mark

发射时检测接口方向的正确方法是什么?

  •  6
  • Mark  · 技术社区  · 15 年前

    [问题更新]

    所以问题就在这里,我最终把范围缩小到了这个。如果在所有方法中创建新的UIViewController

    - (id)init;
    - (void)loadView;
    - (void)viewDidAppear:(BOOL)animated;
    - (void)viewDidLoad;
    (...)
    

    标准接口定向是纵向的,如果检测到横向模式,它将快速旋转到该方向。然后可以使用:

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
    

    问题是,在iPad上,很难在loadView(或其他任何一个)中为当前的界面方向准备界面,因为它只返回纵向。这将导致一些问题:

    1) 我希望我的内容在portait模式下重新加载,但不是在横向模式下。通常,我会在loadView中放置if语句。如果处于纵向模式,请重新加载内容。但在这种情况下,它总是返回纵向,因此总是加载内容。

    2) 我想在纵向模式下使用'presentPopoverFromRect:inView:permittedArrowDirections:animated:'-方法,以便在应用程序启动时自动显示弹出菜单。以纵向模式启动时,这将使应用程序崩溃。原因:“无法从没有窗口的视图中显示弹出窗口。”。

    唯一安全的假设是在“didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation”中,但如果此方法以纵向模式启动,则不会触发。

    //----

    更新(15.37)

    仅当用户从纵向切换到横向(或反之亦然)时才会发布。如果接口是问题所在,那么通过观察该通知和

    if (UIDeviceOrientationIsPortrait(interfaceOrientation)) {
        // layout subviews for portrait mode
    } else {
        // layout subviews for landscape mode
    }
    

    3 回复  |  直到 15 年前
        1
  •  8
  •   Mark Adams    15 年前

    尝试 [[UIApplication sharedApplication] statusBarOrientation] .

        2
  •  6
  •   Pascal    15 年前

    正确的方法是 装置 因为它作为一个视图的方向在技术上是没有方向的。;)

    所以你用得对 UIDevice . 从 documentation 可以看到,在该信息正确之前,首先需要生成设备定向通知。然后它会按需要工作。

    UIDevice *myDevice = [UIDevice currentDevice];
    [myDevice beginGeneratingDeviceOrientationNotifications];
    UIDeviceOrientation currentOrientation = [myDevice orientation];
    [myDevice endGeneratingDeviceOrientationNotifications];
    

    这将为您提供设备的当前方向。如果当前可见/俯视控制器 允许旋转到这个方向,但是您将得到当前设备的方向,而不是当前使用的最上面的视图控制器的方向。


    编辑 问题更新后:

    最上面的视图控制器的方向(对于子视图控制器工作正常)始终返回1(即。 UIInterfaceOrientationPortrait loadView . 但是方法 willRotateToInterfaceOrientation:duration: 将立即调用,并且传递到那里的方向是正确的,因此您应该能够使用该方法。

    - (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toIfaceOrient
                                     duration:(NSTimeInterval)duration
    
        3
  •  1
  •   Mark Adams    14 年前

    我不确定这是否真的能解决我的问题,但看看文档,我发现了以下几点:

    typedef enum {
       UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
       UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
       UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
       UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
    } UIInterfaceOrientation;
    

    typedef enum {
       UIDeviceOrientationUnknown,
       UIDeviceOrientationPortrait,
       UIDeviceOrientationPortraitUpsideDown,
       UIDeviceOrientationLandscapeLeft,
       UIDeviceOrientationLandscapeRight,
       UIDeviceOrientationFaceUp,
       UIDeviceOrientationFaceDown
    } UIDeviceOrientation;
    

    当然,UIViewController也有“interfaceOrientation”选项。

    推荐文章