最近閑來獨自搞了個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。