UIViewAutoresizing

C3E2BBAE-0C93-471C-B6BF-32DC42A8748F.png

最近閑來獨自搞了個app上架,目前還在審核, 該應(yīng)用在很多場景下都用了自定義的彈出視圖,所有抽空把它抽出來做成一個單獨 PopupView 控件,已經(jīng)放在github,支持cocoapods了,歡迎 star,哈哈,這都是閑話,主要是控件涉及界面相關(guān)的內(nèi)容,平常都用習(xí)慣了 Masonry 了,再加上自己畢業(yè)后就開始寫SDK,界面寫得還真的不多,控件開源出去就不方便用 Masonry 了,只能用原生的了。

作為一名程序猿,不懂不恥,現(xiàn)學(xué)現(xiàn)賣 _

先看 UIViewAutoresizing 內(nèi)容:

typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
  • UIViewAutoresizingNone:就是不改變大小,不解釋
  • UIViewAutoresizingFlexibleLeftMargin:調(diào)整與父視圖左邊距,保證右邊距不變

這個就是相當(dāng)于寬度固定時,Masonry 中:

make.right.mas_equalTo(weakSelf.view.mas_left).offset(-OFFSET);
  • UIViewAutoresizingFlexibleRightMargin: 調(diào)整與父視圖右邊距,保證左邊距不變,和UIViewAutoresizingFlexibleLeftMargin 正好相反
  • UIViewAutoresizingFlexibleWidth:就是上面兩個的結(jié)合體,左右邊距不變,改變大小適應(yīng), 相當(dāng)于:
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin

注意,可以通過 | 結(jié)合多個條件來完成所要的效果。

下面就不啰嗦了:

  • UIViewAutoresizingFlexibleTopMargin:下邊距不變,上邊距適應(yīng)
  • UIViewAutoresizingFlexibleBottomMargin:和UIViewAutoresizingFlexibleTopMargin相反
  • UIViewAutoresizingFlexibleHeight:
UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin

這里就不過多閑話了,demo 源碼直接貼出:

@interface ViewController ()
@property(nonatomic, strong) UIView *subview1;
@property(nonatomic, strong) UIView *subview2;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareSubview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - prepare method
- (void)prepareSubview {
    [self.view addSubview:self.subview1];
    self.subview2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.subview1 addSubview:self.subview2];
}

#pragma mark - setter
- (UIView *)subview1 {
    if (!_subview1) {
        _subview1 = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 300, 300)];
        _subview1.backgroundColor = [UIColor blueColor];
    }
    return _subview1;
}

- (UIView *)subview2 {
    if (!_subview2) {
        _subview2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
        _subview2.backgroundColor = [UIColor redColor];
    }
    return _subview2;
}

- (IBAction)buttonClick:(id)sender {
    UIButton *button = (UIButton *)sender;
    if (!button.isSelected) {
        self.subview1.frame = CGRectMake(0, 100, 400, 400);
        button.selected = YES;
    } else {
        self.subview1.frame = CGRectMake(0, 100, 300, 300);
        button.selected = NO;
    }  
}
@end

這里通過設(shè)置 self.subview2.autoresizingMask 的值去查看各種效果,使用過 autolayout 的朋友應(yīng)該都能秒上手了。

不為不會而恥,只因不學(xué)而愧,生命不息學(xué)習(xí)不止,偶爾打點小雞血_ YHPopupView 歡迎各位star & fork,準(zhǔn)備這周出 PopupView 形式的自定義AlertView。

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

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

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