Autoresizing的问题

  • 一般情况下,以下这些view的autoresizingMask默认就是18(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)

    • 1.从xib里面创建出来的默认控件(Xcode自动创建出来的那个view)
    • 2.控制器的view
  • 如果不希望控件拥有autoresizingMask的自动伸缩功能,应该设置为none

  • ViewController

// ViewController.h
#import 

@interface ViewController : UIViewController


@end

// ViewController.m
#import "ViewController.h"

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

 如果不希望控件拥有autoresizingMask的自动伸缩功能,应该设置为none
 blueView.autoresizingMask = UIViewAutoresizingNone;
 */

@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 *)touches withEvent:(UIEvent *)event {
    CGRect frame = self.redView.frame;
    frame.size.width -= 10;
    frame.size.height += 10;
    self.redView.frame = frame;
}

@end


Autoresizing的问题_第1张图片
  • 显示效果图片:
Autoresizing的问题_第2张图片

Autoresizing的问题_第3张图片

你可能感兴趣的:(Autoresizing的问题)