在我的應用程序中,我既有tabbar也有navigationBar。 rootview控制器的tabbar和tabbar有4個導航控制器。某些視圖控制器的固定方向
我想讓一些viewcontrollers只是肖像。這可能是一個常見的問題,但我已經嘗試過了,但我無法解決這個問題。
如何使一些視圖控制器的縱向方向?
在我的應用程序中,我既有tabbar也有navigationBar。 rootview控制器的tabbar和tabbar有4個導航控制器。某些視圖控制器的固定方向
我想讓一些viewcontrollers只是肖像。這可能是一個常見的問題,但我已經嘗試過了,但我無法解決這個問題。
如何使一些視圖控制器的縱向方向?
我已經解決了這個問題並回答了,如果有人遇到同樣的問題,他們可以得到幫助。
shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
如果上述方法位於navigationcontroller的任何tabbarcontroller中,則不會調用viewcontroller。如果這些方法在tabbarcontroller或導航控制器內部聲明,那麼它們將被調用。在我的例子中,viewcontrollers在navigationcontroller裏面,導航控制器在tabbarcontroller裏面。
爲了解決這個問題,我創建了一個類FixedOrientationTab,它是UITabBarController的一個子類,以及導航類OrientationEnabledNavigation,它是UINavigationController的一個子類。然後我在FixedOrientationTab和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
FixedOrientationTab.h
#import <UIKit/UIKit.h>
@interface FixedOrientationTab : UITabBarController
@end
FixedOrientationTab.m
#import "FixedOrientationTab.h"
@interface FixedOrientationTab()
@end
@implementation FixedOrientationTab
- (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.selectedViewController shouldAutorotate];
// return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
然後,如果你想在你的項目中使用導航控制器,然後使用OrientationEnabledNavigation和的TabBar FixedOrientationTab。之後,如果您在視圖控制器中實現了shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
這些方法,那麼它們將被調用。
希望這會有所幫助.. :)