我想在几个UIViewcontroller中锁定方向
1)Loginview(仅限支持肖像)具有登录按钮的UIViewcontroller
2)在登录按钮上,我从故事板调用UITabbarController,有3个标签,所有标签都嵌入了UINavigationControllers。(Tab1,Tab2,Tab3)
3)在具有按钮button1的Tab1(支持横向和纵向)上,我从中推动UIViewcontroller演示(仅支持肖像)。如下所示
Demo *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Demo"];
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
Demo已成功加载,但我想将其旋转锁定为肖像,所以我创建了一个UINavigationController类别,但它仍在旋转。
#import <UIKit/UIKit.h>
@interface UINavigationController (Orientation)
@end
// ==========
#import "UINavigationController+Orientation.h"
@implementation UINavigationController (Orientation)
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortraitUpsideDown;;
}
@end
// -------在我的演示uiviewcontroller中我但是在代码之下却从未调用过--------
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
我看了很多帖子,但没有帮助,请帮帮我怎么做?
答案 0 :(得分:2)
仅在目标中的初始设置方向为纵向 - &gt;一般 - &gt;部署信息(见下图)
在 AppDelegate.h 声明属性:
@property (assign, nonatomic) BOOL shouldRotate;
在 AppDelegate.m 中定义
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.shouldRotate)
return UIInterfaceOrientationMaskAllButUpsideDown;
else
return UIInterfaceOrientationMaskPortrait;
}
在 UIViewController Implemenatation 中设置shouldRotate值,如:
如果要旋转,请设置YES
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] setShouldRotate:YES];
}
否则设置NO
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] setShouldRotate:NO];
}
注意: setShouldRotate:始终在 viewDidAppear:中调用。因此,无论何时切换UIViewController,它都能正常工作。
希望这对你有用。一切顺利:)