iOS設置整體支持豎屏,部分頁面可支持橫屏 – iPhone手機開發 iPhone軟體開發教學課程

iOS設置整體支持豎屏,部分頁面可支持橫屏:在iOS開發中有時候會遇到部分頁面支持橫屏,如視頻播放頁。但是大部分頁面支持豎屏。具體操作及代碼如下。
一:首先項目的targets中需要支持左旋轉,右旋轉。

二:AppDelegate的.h文件添加一個屬性allowRotation控制是否允許旋轉

#import 
#import 

@interface AppDelegate : UIResponder 

@property (strong, nonatomic) UIWindow *window;
/**
 * 是否允許旋轉 YES:允許 NO:不允許
 */
@property(nonatomic,assign) BOOL allowRotation;

@end
三:AppDelegate的.m文件修改這個方法- (UIInterfaceOrientationMask)application:(UIApplication )application supportedInterfaceOrientationsForWindow:(nullable UIWindow )window NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if (self.allowRotation) {
        return UIInterfaceOrientationMaskAll;
    }

    return UIInterfaceOrientationMaskPortrait;
}
四:在需要設置旋轉的控制器中引入頭文件:#import “AppDelegate.h”
頁面出現時調用下面代碼,要把appdelegate.allowRotation置YES

 AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
 appdelegate.allowRotation=YES;
退出界面時調用下面代碼,要把appdelegate.allowRotation置NO

 AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
 appdelegate.allowRotation=NO;

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *