【问题标题】:How to load UIView in Portrait mode irrespective of device orientation - IOS 6无论设备方向如何,如何在纵向模式下加载 UIView - IOS 6
【发布时间】:2012-12-17 18:37:53
【问题描述】:

我正在开发 iOS 6 应用程序,我使用以下方法处理了 UIview 旋转...

-(NSUInteger)supportedInterfaceOrientations

{
    return UIInterfaceOrientationMaskPortrait;
}


- (BOOL) shouldAutorotate {
    return YES;
}

一切正常,问题是初始视图加载是根据设备方向决定的,例如:如果我将设备保持在横向模式,即使我在supportedInterfaceOrientations 视图中强制返回纵向,一旦设备显示为横向之后旋转为纵向,它不会进入横向模式,一切正常。无论设备方向如何,是否可以在特定模式下加载视图?

我用谷歌搜索过,没有任何效果。

注意:我正在使用导航控制器。我还为 UINavigationController 添加了类别。

// IOS6 中用于处理方向的自定义类别

@implementation UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end

谢谢

【问题讨论】:

    标签: objective-c ios xcode cocoa-touch uiview


    【解决方案1】:

    使用这个方法:

     - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
      return UIInterfaceOrientationMaskPortrait;
    }
    

    将此方法与category 一起添加到appDelegate 中的UINavigationController

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
      UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
      //check for which controller u need these methods
      if([navigationController.visibleViewController isKindOfClass:[yourViewController class]]) //provide specific view controller where u want protrait
      {
        [navigationController shouldAutorotate];
        [navigationController supportedInterfaceOrientations];
        [navigationController preferredInterfaceOrientationForPresentation];
      }
      return UIInterfaceOrientationMaskAll;
    }
    

    注意:如果您为UINavigationController 添加了category,则从所有viewController 中删除。它应该只在 appDelegate 中。

    【讨论】:

    • 嗨王子.. 昨天的问题是 iPad mini 的按钮,一旦我切换它就可以正常工作...... :)。今天的问题:在哪里加这个,之前在对应的视图里试过了。。还是不行。
    • 控制器中的函数没有被调用。
    • 我已将其添加到类别中,并且在最初加载时未命中首选接口OrientationForPresentation。
    • - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 这个函数被我多次调用,每当它被调用 category 方法也被调用,再次回到同一个函数...导致最终挂起和崩溃,类别仅在 app delegate.mm 中
    • 请检查我给类别的编辑我是如何使用的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2012-11-03
    • 1970-01-01
    相关资源
    最近更新 更多