objective-c - 在 iOS6 中处理旋转

标签 objective-c uiviewcontroller uinavigationcontroller orientation ios6

我只想在我的 UINavigationController 堆栈中的一个 View 上支持不同的方向。我怎样才能做到这一点?

它也必须在 iOS5 中工作。

最佳答案

我在 iOS6 处理 Orientation 的方式上遇到了很多麻烦,希望这就是您要找的。

创建一个 UINavigationController 类并命名为“UINavigationController+autoRotate”。

将其放入您的 UINavigationController+autoRotate.h 中:

#import <UIKit/UIKit.h>

@interface UINavigationController (autoRotate)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
-(BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;

@end

将其放入 UINavigationController+autoRotate.m 中:

#import "UINavigationController+autoRotate.h"

@implementation UINavigationController (autoRotate)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

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


- (NSUInteger)supportedInterfaceOrientations
{
    if (![[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"ViewController")])
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
}

@end

对于您不想旋转的 View ,添加:

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

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

对于您确实想要旋转的 View :

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIDeviceOrientationPortraitUpsideDown);
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

在您应用的委托(delegate)中,添加:

- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

关于objective-c - 在 iOS6 中处理旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12780925/

相关文章:

objective-c - viewDidDisappear 没有在 UINavigationController 上被调用

swift - 单击单元格后,它向我传递了 2 个 View ,我该如何修复它?

iphone - UIpopover 中带提示的导航栏

ios - 在 Segue 中但在 ViewDidLoad 中设置属性返回 null

ios - UIDatePicker NSRangeException 崩溃 iOS 11

ios - 在 iOS 中将值从子 ViewController 传递到父 ViewController 的最佳方法是什么?

ios - BAD_EXC_ACCESS 用于在释放时尝试加载 View Controller

ios - 为什么applicationFrame高度为460时窗口480的高度?

objective-c - 如何在 UITableViewCell 的 UITextView 中一致地绘制 NSAttributedString

iphone - 带有自定义容器 View Controller (MMDrawerController) 的 iOS 状态恢复