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 22:15

My situation is:

  • UITabBarController has 2 items: 2 Navigation controller
  • UINavigationController1 withRootView: ViewController1
  • UINavigationController2 withRootView: ViewController2.
  • Now i want ViewController1 set shouldAutorotate:NO/maskPortrait and ViewController2 set shouldAutorotate/MaskAll.

So my implement: Create UITabBarController category and UINavigationCntroller category like

UITabBarController+autoRotate.h

@interface UITabBarController (autoRotate)

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

@end

UITabBarController+autoRotate.m

#import "UITabBarController+autoRotate.h"

@implementation UITabBarController (autoRotate)

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
    return [self.selectedViewController supportedInterfaceOrientations];
}

@end

UINavigationController+autoRotate.h

@interface UINavigationController (autoRotate)

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

@end

UINavigationController+autoRotate.m

@implementation UINavigationController (autoRotate)

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

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

@end

UIViewController1.m

- (BOOL)shouldAutorotate {
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
   return UIInterfaceOrientationMaskPortrait;;
}

UIViewController2.m

- (BOOL)shouldAutorotate {
   return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
   return UIInterfaceOrientationMaskAll;
}

It worked like a charm!

查看更多
姐就是有狂的资本
3楼-- · 2019-01-10 22:16

If you have a tab bar like I did, the only thing you need to do is to add the following to your delegate .m file,

#import "AppDelegate.h"

//UITabBarController category to set the view rotations for ios 6
@implementation UITabBarController (Background)

-(BOOL)shouldAutorotate
{
    //I don't want to support auto rotate, but you can return any value you want here
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    //I want to only support portrait mode
    return UIInterfaceOrientationMaskPortrait;
}

@end


/////here starts the implementation of the app delegate which is gonna be whatever you currently have on your .m delegate

@implementation AppDelegate

// delegate methods and other stuff

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