我有某些viewControllers
,它们由UINavigationController
(推和弹出)管理。我想将不同的viewControllers
限制为不同的orientations
,例如第一个应该仅在Portrait
中,第二个在portrait
中,第三个在landscape
中,第四个可以在portrait
和landscape
中。我在ViewController
的isInitialViewController
上设置了storyBoard
,
- (BOOL) shouldAutorotate{
return NO;
}
可以正常工作,但是当我将
navigation controller
(通过推和弹出来管理这四个视图)从isInitialViewController
设置为storyBoard
时,此函数停止了调用,现在是autoratates
。如何使用此autorotating
作为UINavigationController
停止isInitialViewController
这些视图。我使用以下功能取决于它是哪个ViewController
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation == UIDeviceOrientationPortrait);//choose portrait or landscape}
- (BOOL) shouldAutorotate{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
//return UIInterfaceOrientationMaskLandscape;
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
// return UIInterfaceOrientationLandscapeLeft |
// UIInterfaceOrientationLandscapeRight;
return UIInterfaceOrientationPortrait;
}
最佳答案
只是子类UINavigationController并重写适当的方法:
.h文件:
@interface CustomUINavigationController : UINavigationController
@property BOOL canRotate;
@end
.m文件:
@implementation CustomUINavigationController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
return self.canRotate;
}
@end