版本記錄
| 版本號 | 時間 |
|---|---|
| V1.0 | 2017.08.04 |
前言
??很多時候我們的app都需要橫豎屏的適配,并且根據(jù)手機的方向自動的更改屏幕的方向,下面我們就說一下屏幕橫豎屏適配的方法。前面我也寫過類似的一篇文章,但是有人說寫的有點啰嗦,所以我又寫了一個簡化的demo,希望對大家有所幫助,下面先給出上一篇文章相關鏈接。
1. 實用小技巧(二):屏幕橫豎屏的判斷和相關邏輯
適配前提和基礎
在我們進行橫豎配之前我們需要幾個前提和基礎。
1. 交互方向
交互方向是一個枚舉值,UIInterfaceOrientation,大家可以看一下
//狀態(tài)欄方向,可以看見橫屏時和設備方向是反的
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;
2. 設備方向
這個和上面那個方向不是一回事,這個也是一個枚舉UIDeviceOrientation,下面我們還是看一下
//設備方向,具體可以參考home鍵的位置,這里給的已經(jīng)很清楚了
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
} __TVOS_PROHIBITED;
3. 設備橫屏后狀態(tài)欄的顯示
默認情況下,橫屏以后是不會顯示狀態(tài)欄的,所以我們要采取下面的措施。
第一步:設置info.plist
如下圖所示。

info.plist配置
第二步:代碼階段
在要橫屏顯示的控制器里面viewDidLoad里面加上如下代碼。
//橫屏顯示狀態(tài)欄 ios9以后已經(jīng)棄用,后面會有替代方法介紹
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
適配代碼實現(xiàn)
下面看一下適配的代碼。
1. JJScreenOritentionVC.m
#import "JJScreenOritentionVC.h"
#import "Masonry.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface JJScreenOritentionVC ()
@property (nonatomic, strong) UILabel *label;
@end
@implementation JJScreenOritentionVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupUI];
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ||
[UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[self layoutPortraitView];
}
else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ||
[UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight){
[self layoutLandscapeView];
}
//監(jiān)聽屏幕的旋轉方向
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOritentionDidChanged) name:UIDeviceOrientationDidChangeNotification object:nil];
//橫屏顯示狀態(tài)欄 ios9以后已經(jīng)棄用,后面會有替代方法介紹
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.navigationBarHidden = NO;
}
#pragma mark - Object Private Function
- (void)setupUI
{
self.view.backgroundColor = [UIColor yellowColor];
UILabel *label = [[UILabel alloc] init];
label.text = @"這個可怎么破?";
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor colorWithRed:0.3 green:0.6 blue:0.8 alpha:1.0];
[self.view addSubview:label];
self.label = label;
}
//豎屏方向
- (void)layoutPortraitView
{
[self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.width.equalTo(@(kScreenWidth - 20));
make.height.equalTo(@30);
}];
}
//橫屏方向
- (void)layoutLandscapeView
{
//注意:這里一定要用remake更新原先的約束,要不是變不過來的
[self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.width.equalTo(@(kScreenWidth - 20));
make.height.equalTo(@30);
}];
}
#pragma mark - Action && Notification
- (void)deviceOritentionDidChanged
{
UIDeviceOrientation deviceOrient = [UIDevice currentDevice].orientation;
NSLog(@"%ld",deviceOrient);
if (deviceOrient == UIDeviceOrientationPortrait || deviceOrient == UIDeviceOrientationPortraitUpsideDown) {
NSLog(@"豎屏");
//設置豎屏視圖
[self layoutPortraitView];
}
else if(deviceOrient == UIDeviceOrientationLandscapeLeft){
NSLog(@"橫屏");
[self layoutLandscapeView];
}
else if(deviceOrient == UIDeviceOrientationLandscapeRight){
NSLog(@"橫屏");
[self layoutLandscapeView];
}
}
@end
具體代碼就這么多,很簡單吧。
適配效果驗證
下面就看一下適配效果驗證。

豎屏

橫屏
當你改變手機的方向的時候,就會觸發(fā)通知,自使用的更改方向了。
后記
關于屏幕適配,這里是第二篇了,希望能解決大家的基本困惑,關于更深層次這方面的應用,我會后續(xù)繼續(xù)和大家分享,希望對大家有所幫助。

這個和我家還是很像的