Device Orientation:設備方向
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top
UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left
UIDeviceOrientationFaceUp, // Device oriented flat, face up
UIDeviceOrientationFaceDown // Device oriented flat, face down
}
UIInterfaceOrientation:界面方向
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
}
屏幕旋轉的兩個通知
// 硬件旋轉通知,此時界面還未完成旋轉可設置界面的旋轉
UIDeviceOrientationDidChangeNotification
// 界面旋轉通知,此時界面已經(jīng)完成旋轉
UIApplicationDidChangeStatusBarOrientationNotification
判斷當前界面方向
BOOL isPortrait = UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]);
BOOL isLandscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
單個頁面是否可以旋轉
@property(nonatomic, readonly) BOOL shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
@property(nonatomic, readonly) UIInterfaceOrientationMask supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
-
面向Orientation的信息:隨著屏幕的方向變化
- [UISCreen bounds]
- [UISCreen applicationFrame]
- Status bar frame notifications
- Keyboard frame notifications
屏幕旋轉時使得View有不同布局的實現(xiàn)方法
// way1
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize: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 {
[super viewDidLayoutSubviews];
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);
}
}
- 旋轉屏幕方向
NSNumber *value0 = [NSNumber numberWithInteger:UIDeviceOrientationUnknown];
[[UIDevice currentDevice] setValue:value0 forKey:@"orientation"];
NSNumber *value = [NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
- 需求:APP總體不跟隨設備的旋轉來改變界面的方向,個別頁面橫屏展示
實現(xiàn)思路:首先APP應該支持橫屏和豎屏的展示狞谱,也就是說在TARGETS中的Device Orientation選項勾選橫屏和豎屏選項瞎领。假設ViewController是根視圖挟阻,那么當ViewController的- (BOOL)shouldAutorotate方法返回為YES的時候毅整,界面的方向跟隨設備方向變化。在NV結構(NavigationController上放置ViewController的結構)中需橫屏展示的ViewController可不是根視圖木羹,此時界面是否跟隨設備旋轉取決于根視圖NavigationController的- (BOOL)shouldAutorotate方法的返回值。在這種情況中解孙,可重寫NavigationController的- (BOOL)shouldAutorotate方法返回Navigation棧中的頂部ViewController的shouldAutorotate坑填,以此達到在單個頁面隨時控制其橫屏的效果。橫屏通過Key-Value的方法修改設備方向來達到目的弛姜。
一脐瑰、勾選橫屏和豎屏選項
二、重寫- (BOOL)shouldAutorotate返回棧頂控制器的shouldAutorotate
@interface MyNav : UINavigationController
@end
@implementation MyNav
- (BOOL)shouldAutorotate {
return self.topViewController.shouldAutorotate;
}
@end
三廷臼、在將要橫屏顯示的ViewController中苍在,重寫- (BOOL)shouldAutorotate绝页,重點是用全局變量控制它的返回值
@interface LandscapeViewController () {
BOOL _allowAutorotate;
}
- (BOOL)shouldAutorotate {
[super shouldAutorotate];
return _allowAutorotate;
}
四、通過Key-Value改變設備方向
_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;