autoresizingMask

2016-05-25  本文已影响97人  武一顶顶

如果不希望控件拥有autoresizingMask的自动伸缩功能,应该设置为none

 blueView.autoresizingMask = UIViewAutoresizingNone;
#import "ViewController.h"
@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;
    // 去掉默认的自动拉伸宽高功能
    //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;

}
//  0001
//  0010

@end
Paste_Image.png Paste_Image.png
上一篇 下一篇

猜你喜欢

热点阅读