最近闲来独自搞了个app上架,目前还在审核, 该应用在很多场景下都用了自定义的弹出视图,所有抽空把它抽出来做成一个单独 PopupView 控件,已经放在github,支持cocoapods了,欢迎 star,哈哈,这都是闲话,主要是控件涉及界面相关的内容,平常都用习惯了 Masonry 了,再加上自己毕业后就开始写SDK,界面写得还真的不多,控件开源出去就不方便用 Masonry 了,只能用原生的了。
作为一名程序猿,不懂不耻,现学现卖 _
先看 UIViewAutoresizing 内容:
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:调整与父视图左边距,保证右边距不变
这个就是相当于宽度固定时,Masonry 中:
make.right.mas_equalTo(weakSelf.view.mas_left).offset(-OFFSET);
- UIViewAutoresizingFlexibleRightMargin: 调整与父视图右边距,保证左边距不变,和UIViewAutoresizingFlexibleLeftMargin 正好相反
- UIViewAutoresizingFlexibleWidth:就是上面两个的结合体,左右边距不变,改变大小适应, 相当于:
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin
注意,可以通过 | 结合多个条件来完成所要的效果。
下面就不啰嗦了:
- UIViewAutoresizingFlexibleTopMargin:下边距不变,上边距适应
- 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
这里通过设置 self.subview2.autoresizingMask 的值去查看各种效果,使用过 autolayout 的朋友应该都能秒上手了。
不为不会而耻,只因不学而愧,生命不息学习不止,偶尔打点小鸡血_ YHPopupView 欢迎各位star & fork,准备这周出 PopupView 形式的自定义AlertView。