以下示範如何讓 App 中個別頁面強制橫屏,其餘頁面保持豎屏。
在 AppDelegate.h 中新增 @property(nonatomic,assign)NSInteger allowRotation;
在 AppDelegate.m 中加入:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (_allowRotation == 1) { return UIInterfaceOrientationMaskLandscapeRight; } else { return (UIInterfaceOrientationMaskPortrait); } }如此一來,預設所有頁面都是豎屏。接著在需要強制橫屏的頁面控制器 UIViewController 中,引入 #import "AppDelegate.h",然後加入:
- (void)viewDidLoad { [super viewDidLoad]; AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; appDelegate.allowRotation = 1; }即可讓個別頁面單獨橫屏。在離開該橫屏頁面前,記得還原狀態:
AppDelegate *delegate = [[UIApplication sharedApplication]delegate]; delegate.allowRotation = 0;
如此便能完整實現頁面的橫屏切換。以上就是 iOS 中讓個別頁面強制橫屏、其他頁面維持豎屏的做法,希望對大家有所幫助。若有任何疑問,歡迎留言。