autoresizingMask

  • 一般情况下,以下这些view的autoresizingMask默认就是18(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)
  • 1.从xib里面创建出来的默认控件(Xcode自动创建出来的那个view)
  • 2.控制器的view

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

 blueView.autoresizingMask = UIViewAutoresizingNone;
  • demo
#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
  • 示例图

    • 默认,没有设置为none时:
Paste_Image.png
  • 设置为 UIViewAutoresizingNone时: (代码示例)
Paste_Image.png

推荐阅读更多精彩内容