5月31日-Autoresizing

屏幕適配的發(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,185評論 4 61
  • 黑夜忽然變得很漫長仿佛是一生一輩子那樣總也走不完除了心臟孤獨(dú)跳動的聲音世界猶如在靜止的蠻荒思緒被這份沉重壓迫無助與...
    丁_香閱讀 696評論 38 88
  • 天雨雖大,不潤無根之草,佛法雖寬,不渡無緣之人。機(jī)會都是從相信和行動開始,我的機(jī)會來了,一定要加油把握。
    呂明超閱讀 109評論 0 0

友情鏈接更多精彩內(nèi)容