shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
上述方法不會被調用一個UIViewController
的,如果他們是任何UINavigationController
內。如果這些方法在UINavigationController
內部聲明,那麼它們將被調用。
爲了解決這個問題,一個班讓我們稱之爲OrientationEnabledNavigation
,它是UINavigationController
的一個子類。然後在OrientationEnabledNavigation
裏面實施shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
方法。
OrientationEnabledNavigation.h
#import <UIKit/UIKit.h>
@interface OrientationEnabledNavigation : UINavigationController
@end
OrientationEnabledNavigation.m
#import "OrientationEnabledNavigation.h"
@interface OrientationEnabledNavigation()
@end
@implementation OrientationEnabledNavigation
- (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.
}
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
// return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
然後,如果你想在你的項目中使用導航控制器,然後使用OrientationEnabledNavigation
。之後,如果您在視圖控制器中實現了shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
這些方法,那麼它們將被調用。
的在你的ViewController實現這些:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
另一種方法是添加你的應用程序代理的一些代碼開頭:
對於UITabBarController
@implementation UITabBarController (AutoRotationForwarding)
-(BOOL)shouldAutorotate
{
if ([self.selectedViewController respondsToSelector:@selector(shouldAutorotate)]) {
return [self.selectedViewController shouldAutorotate];
}
else {
return YES;
}
}
- (NSUInteger)supportedInterfaceOrientations {
if ([self.selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
return [self.selectedViewController supportedInterfaceOrientations];
}
else {
return UIInterfaceOrientationMaskAll;
}
}
@end
對於UINavigationController
@implementation UINavigationController (AutoRotationForwarding)
-(BOOL)shouldAutorotate
{
if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)]) {
return [self.topViewController shouldAutorotate];
}
else {
return YES;
}
}
-(NSUInteger) supportedInterfaceOrientations {
if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
{
return [self.topViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskPortrait;
}
@end
希望這有助於.. :)
我嘗試過,但通過這種方式,我可以顯示所有的細胞相同的控制器。 – yashwanth777
你的意思是你可以推動或者你不能? – Rivera