UITabBarController Rotation Issues in ios 6

2019-01-10 21:45发布

Ack! I had my tabbar rotation issues resolved finally in iOS 5, but iOS 6 and xcode seem to have broken things... here is what I have:

Target App Summary includes: Supported Interface Orientations - Portraint, Landscape Left, Landscape Right

Every Single View in the App has the following methods:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    return ((interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown) &&
            (interfaceOrientation != UIInterfaceOrientationLandscapeLeft) &&
            (interfaceOrientation != UIInterfaceOrientationLandscapeRight));
} else {
    return YES;
}
}

- (BOOL)shouldAutorotate
{
NSLog(@"am I called1?");
return NO;
}

-(NSUInteger)supportedInterfaceOrientations{
   NSLog(@"am I called?");
   return UIInterfaceOrientationMaskPortrait;
}

In the views that are not part of the tab bar, rotation is blocked. In ALL the views of the tabbar (there are 5) the app never calls ShouldAutorotate and so rotates. It does seem supportedInterfaceOrientations gets called once when a view loads, but not when it appears if I switch between views, because I get the NSLog, but it seems to ignore the MaskPortrait setting.

I have to leave the landscape enabled in the target because I have a single video player view that needs to rotate (and it does so, fine)

Is this a tabbar bug in iOS 6? Do I need to disable the rotation of the views differently? The shouldautorotatetointerfaceorientation worked great in ios 5

I've been at it for a while

Thanks, Zack

8条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-10 21:50

I also had the issue that I needed some views to rotate and others not with several Navigation Controllers. I did this by telling the NavigationController to look in the view controller. Here is what I did.

I create a UINavigationController class called RootNavigationController and designated that class as the Custom Class for the Navigation Controllers in storyboard. In the RootNavigationController.m I added the following methods;

- (BOOL)shouldAutorotate {

    return [self.visibleViewController shouldAutorotate];
}

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

In each view controller .m file I also added the following methods.

- (BOOL)shouldAutorotate {
    //return yes or no
}

- (NSUInteger)supportedInterfaceOrientations{
    //return supported orientation masks
}

Doing this allows me to set orientation for each view in its view controller.

Worked for me anyway…

查看更多
干净又极端
3楼-- · 2019-01-10 21:51

You should add this thing in the UIViewController1.m to make sure that the previous orientation status is reconstructed:

 (void)viewDidAppear:(BOOL)animated 
{

    [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") 
    withObject:(id)UIInterfaceOrientationPortrait];
}

Perfect!

查看更多
可以哭但决不认输i
4楼-- · 2019-01-10 21:52

In my case, I had a navigation controller embedded within a UITabBarController, and what worked was creating a category like Kunani defined, but extendind UITabBarController instead of UINavigationController. It worked like a charm :)

#import "UINavigationController+autoRotate.h"

@implementation UINavigationController (autoRotate)

-(BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

@end

And .h file:

#import <UIKit/UIKit.h>

@interface UINavigationController (autoRotate)

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

@end
查看更多
Luminary・发光体
5楼-- · 2019-01-10 22:01

This will do it in Swift

extension UITabBarController {
    public override func shouldAutorotate() -> Bool {
        if let selected = self.selectedViewController {
            return selected.shouldAutorotate()
        } else {
            return false
        }
    }

    public override func supportedInterfaceOrientations() -> Int {
        if let selected = self.selectedViewController {
            return selected.supportedInterfaceOrientations()
        } else {
            return Int(UIInterfaceOrientationMask.Portrait.rawValue)
        }
    }
}

extension UINavigationController {
    public override func shouldAutorotate() -> Bool {
        return self.visibleViewController.shouldAutorotate()
    }

    public override func supportedInterfaceOrientations() -> Int {
        return self.visibleViewController.supportedInterfaceOrientations()
    }
}
查看更多
虎瘦雄心在
6楼-- · 2019-01-10 22:02

https://stackoverflow.com/a/30632505/2298002

there are a lot of ambiguous or unforeseen results when handling this situation, especially when it comes to maintaining the orientation for only a specific view controller and not effecting the rest of the app. this algorithm seems to hand all for me

https://stackoverflow.com/a/30632505/2298002

查看更多
爷的心禁止访问
7楼-- · 2019-01-10 22:04

Zack, I ran into this same issue. It's because you have your viewController embedded inside of a TabBar Controller or UINavigationController and the calls to these methods are happening inside those instead of your normal View (Changed in iOS6).

I ran into this issue because I was presenting a viewController embedded inside a UINavigationController on all my modal views that had Navigation to different views (Signup Process, Login, etc).

My simple fix was to create a CATEGORY for UINavigationController that includes these two methods. I have shouldAutorotate returning NO anyway because I don't want my modal views rotating. Your fix may be this simple, give it a try. Hope it helps.

I created a category and named it autoRotate and selected theUINavigationController option. The M+H file are below.

#import "UINavigationController+autoRotate.h"

@implementation UINavigationController (autoRotate)

-(BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

@end

... and the category .h:

#import <UIKit/UIKit.h>

@interface UINavigationController (autoRotate)

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

@end
查看更多
登录 后发表回答