屏幕適配的發(fā)展歷史
iPhone3GS\iPhone4
- 沒有屏幕適配可言
- 全部用frame、bounds、center進(jìn)行布局
- 很多這樣的現(xiàn)象:坐標(biāo)值、寬度高度值全部寫死
UIButton *btn1 = [[UIButton alloc] init]; btn1.frame = CGRectMake(0, 0, 320 - b, 480 - c);
iPad出現(xiàn)、iPhone橫屏
- 出現(xiàn)Autoresizing技術(shù)
- 讓橫豎屏適配相對簡單
- 讓子控件可以跟隨父控件的行為自動發(fā)生相應(yīng)的變化
- 前提是:關(guān)閉Autolayout功能
- 局限性
- 只能解決子控件跟父控件的相對關(guān)系問題
- 不能解決兄弟控件的相對關(guān)系問題
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0, 距離父控件左邊的間距是伸縮的(不固定的)
UIViewAutoresizingFlexibleRightMargin = 1 << 2, 距離父控件右邊的間距是伸縮的(不固定的)
UIViewAutoresizingFlexibleTopMargin = 1 << 3, 距離父控件頂部的間距是伸縮的(不固定的)
UIViewAutoresizingFlexibleBottomMargin = 1 << 5, 距離父控件底部的間距是伸縮的(不固定的)
UIViewAutoresizingFlexibleWidth = 1 << 1, 寬度跟隨父控件的寬度進(jìn)行自動伸縮
UIViewAutoresizingFlexibleHeight = 1 << 4, 高度跟隨父控件的高度進(jìn)行自動伸縮
演示代碼:
#import "ViewController.h"
@interface ViewController ()
/** 藍(lán)色 */
@property (nonatomic, strong) UIView *blueView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 創(chuàng)建藍(lán)色UIView
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
blueView.frame = CGRectMake(0, 0, 250, 250);
[self.view addSubview:blueView];
self.blueView = blueView;
// 創(chuàng)建紅色UIView
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(0, 150, 250, 100);
redView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[blueView addSubview:redView];
}
// 點(diǎn)擊View就會自動調(diào)用
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 在100范圍內(nèi)隨機(jī)產(chǎn)生值
CGFloat w = 200 + arc4random_uniform(100);
CGFloat h = 200 + arc4random_uniform(100);
self.blueView.frame = CGRectMake(0, 0, w, h);
}
iOS 6.0(Xcode4)開始
- 出現(xiàn)了Autolayout技術(shù)
- 從Xcode5.0(iOS 7.0)開始,開始流行Autolayout