在父視圖控制器里面寫如下代碼
-(void)setViewOrientation:(UIInterfaceOrientation )orientation { if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)orientation]; } [UIViewController attemptRotationToDeviceOrientation];//這句是關鍵 } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } -(BOOL)shouldAutorotate { return YES; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return toInterfaceOrientation==UIInterfaceOrientationPortrait; }
在子視圖控制器的加入,這里子視圖控制器要橫屏
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear :YES]; [self setViewOrientation:UIInterfaceOrientationLandscapeRight]; }
//重寫下面子類的方法
- (NSUInteger) supportedInterfaceOrientations{
returnUIInterfaceOrientationMaskLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0){
return toInterfaceOrientation == UIInterfaceOrientationLandscapeRight;
}