Autoresizing的問題

  • 一般情況下,以下這些view的autoresizingMask默認(rèn)就是18(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)

    • 1.從xib里面創(chuàng)建出來的默認(rèn)控件(Xcode自動(dòng)創(chuàng)建出來的那個(gè)view)
    • 2.控制器的view
  • 如果不希望控件擁有autoresizingMask的自動(dòng)伸縮功能,應(yīng)該設(shè)置為none

  • ViewController

// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

// ViewController.m
#import "ViewController.h"

/*
 一般情況下,以下這些view的autoresizingMask默認(rèn)就是18(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)
 1.從xib里面創(chuàng)建出來的默認(rèn)控件(Xcode自動(dòng)創(chuàng)建出來的那個(gè)view)
 2.控制器的view

 如果不希望控件擁有autoresizingMask的自動(dòng)伸縮功能,應(yīng)該設(shè)置為none
 blueView.autoresizingMask = UIViewAutoresizingNone;
 */

@interface ViewController ()
/** redView */
@property (nonatomic, weak) UIView *redView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    redView.frame = CGRectMake(20, 20, 300, 200);
    [self.view addSubview:redView];
    self.redView = redView;

    UIView *blueView = [[NSBundle mainBundle] loadNibNamed:@"BlueView" owner:nil options:nil].firstObject;
    // 去掉默認(rèn)的自動(dòng)拉伸寬高功能
//    blueView.autoresizingMask = UIViewAutoresizingNone;
    [redView addSubview:blueView];

    UIView *yellowView = [[UIView alloc] init];
    yellowView.backgroundColor = [UIColor yellowColor];
    yellowView.frame = CGRectMake(0, 0, 200, 150);
    [redView addSubview:yellowView];

    // 18 == UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
    // 0 == UIViewAutoresizingNone
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGRect frame = self.redView.frame;
    frame.size.width -= 10;
    frame.size.height += 10;
    self.redView.frame = frame;
}

@end


  • 顯示效果圖片:

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

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

  • 前言 iPhone自誕生以來,隨著其屏幕尺寸不斷的多樣化,屏幕適配的技術(shù)一直在發(fā)展更新。目前,iOS系統(tǒng)版本已經(jīng)更...
    VV木公子閱讀 15,617評(píng)論 24 170
  • 1.尺寸適配1.原因 iOS7中所有導(dǎo)航欄都為半透明,導(dǎo)航欄(height=44)和狀態(tài)欄(height=20)不...
    LZM輪回閱讀 6,384評(píng)論 1 4
  • iOS XIB使用Safe Area后在iOS9和10上面出現(xiàn)的問題和解決方案 1.多添加一個(gè)距離SuperVie...
    下雨之後閱讀 961評(píng)論 0 1
  • 星期三 晴 每日一我 今天5點(diǎn)半起床,刷微信,曬衣服,完成英語早讀。刷微信的時(shí)間要砍去。 中午去上杠鈴操,站在杠鈴...
    sophietyl閱讀 157評(píng)論 0 0
  • forwardRef 的作用 Angular 通過引入 forwardRef 讓我們可以在使用構(gòu)造注入時(shí),使用尚未...
    semlinker閱讀 1,345評(píng)論 0 2

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