Device Orientation:設(shè)備方向
typedefNS_ENUM(NSInteger,UIDeviceOrientation){UIDeviceOrientationUnknown,UIDeviceOrientationPortrait,// Device oriented vertically, home button on the bottomUIDeviceOrientationPortraitUpsideDown,// Device oriented vertically, home button on the topUIDeviceOrientationLandscapeLeft,// Device oriented horizontally, home button on the rightUIDeviceOrientationLandscapeRight,// Device oriented horizontally, home button on the leftUIDeviceOrientationFaceUp,// Device oriented flat, face upUIDeviceOrientationFaceDown// Device oriented flat, face down}
UIInterfaceOrientation:界面方向
typedefNS_ENUM(NSInteger,UIInterfaceOrientation){UIInterfaceOrientationUnknown=UIDeviceOrientationUnknown,UIInterfaceOrientationPortrait=UIDeviceOrientationPortrait,UIInterfaceOrientationPortraitUpsideDown=UIDeviceOrientationPortraitUpsideDown,UIInterfaceOrientationLandscapeLeft=UIDeviceOrientationLandscapeRight,UIInterfaceOrientationLandscapeRight=UIDeviceOrientationLandscapeLeft}
屏幕旋轉(zhuǎn)的兩個(gè)通知
// 硬件旋轉(zhuǎn)通知俊戳,此時(shí)界面還未完成旋轉(zhuǎn)可設(shè)置界面的旋轉(zhuǎn)UIDeviceOrientationDidChangeNotification// 界面旋轉(zhuǎn)通知姐浮,此時(shí)界面已經(jīng)完成旋轉(zhuǎn)UIApplicationDidChangeStatusBarOrientationNotification
判斷當(dāng)前界面方向
BOOL isPortrait=UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication]statusBarOrientation]);BOOL isLandscape=UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication]statusBarOrientation]);
單個(gè)頁面是否可以旋轉(zhuǎn)
@property(nonatomic,readonly)BOOL shouldAutorotateNS_AVAILABLE_IOS(6_0)__TVOS_PROHIBITED;@property(nonatomic,readonly)UIInterfaceOrientationMask supportedInterfaceOrientationsNS_AVAILABLE_IOS(6_0)__TVOS_PROHIBITED;
面向Orientation的信息:隨著屏幕的方向變化
[UISCreen bounds]
[UISCreen applicationFrame]
Status bar frame notifications
Keyboard frame notifications
屏幕旋轉(zhuǎn)時(shí)使得View有不同布局的實(shí)現(xiàn)方法
// way1-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{[superviewWillTransitionToSize:size withTransitionCoordinator:coordinator];[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>_Nonnull context){if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)){_testTextField.frame=CGRectMake(50,50,150,50);_testButton.frame=CGRectMake(50,120,150,50);}else{_testTextField.frame=CGRectMake(50,50,150,50);_testButton.frame=CGRectMake(220,50,150,50);}}completion:^(id<UIViewControllerTransitionCoordinatorContext>_Nonnull context){}];}
// way2-(void)viewDidLayoutSubviews{[superviewDidLayoutSubviews];if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)){_testTextField.frame=CGRectMake(50,50,150,50);_testButton.frame=CGRectMake(50,120,150,50);}else{_testTextField.frame=CGRectMake(50,50,150,50);_testButton.frame=CGRectMake(220,50,150,50);}}
旋轉(zhuǎn)屏幕方向
NSNumber*value0=[NSNumber numberWithInteger:UIDeviceOrientationUnknown];[[UIDevice currentDevice]setValue:value0 forKey:@"orientation"];NSNumber*value=[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft];[[UIDevice currentDevice]setValue:value forKey:@"orientation"];
需求:APP總體不跟隨設(shè)備的旋轉(zhuǎn)來改變界面的方向,個(gè)別頁面橫屏展示
實(shí)現(xiàn)思路:首先APP應(yīng)該支持橫屏和豎屏的展示垫言,也就是說在TARGETS中的Device Orientation選項(xiàng)勾選橫屏和豎屏選項(xiàng)。假設(shè)ViewController是根視圖瞻讽,那么當(dāng)ViewController的- (BOOL)shouldAutorotate方法返回為YES的時(shí)候鸳吸,界面的方向跟隨設(shè)備方向變化。在NV結(jié)構(gòu)(NavigationController上放置ViewController的結(jié)構(gòu))中需橫屏展示的ViewController可不是根視圖速勇,此時(shí)界面是否跟隨設(shè)備旋轉(zhuǎn)取決于根視圖NavigationController的- (BOOL)shouldAutorotate方法的返回值晌砾。在這種情況中,可重寫NavigationController的- (BOOL)shouldAutorotate方法返回Navigation棧中的頂部ViewController的shouldAutorotate烦磁,以此達(dá)到在單個(gè)頁面隨時(shí)控制其橫屏的效果养匈。橫屏通過Key-Value的方法修改設(shè)備方向來達(dá)到目的。
一都伪、勾選橫屏和豎屏選項(xiàng)
勾選橫屏和豎屏選項(xiàng)
二呕乎、重寫- (BOOL)shouldAutorotate返回棧頂控制器的shouldAutorotate
@interfaceMyNav:UINavigationController@end@implementationMyNav-(BOOL)shouldAutorotate{returnself.topViewController.shouldAutorotate;}@end
三、在將要橫屏顯示的ViewController中陨晶,重寫- (BOOL)shouldAutorotate猬仁,重點(diǎn)是用全局變量控制它的返回值
@interfaceLandscapeViewController(){BOOL _allowAutorotate;}-(BOOL)shouldAutorotate{[supershouldAutorotate];return_allowAutorotate;}
四、通過Key-Value改變設(shè)備方向
_allowAutorotate=YES;NSNumber*value0=[NSNumber numberWithInteger:UIDeviceOrientationUnknown];[[UIDevice currentDevice]setValue:value0 forKey:@"orientation"];NSNumber*value=[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft];[[UIDevice currentDevice]setValue:value forKey:@"orientation"];_allowAutorotate=NO;
轉(zhuǎn)載鏈接:http://www.reibang.com/p/3db323293428