-
一般情況下,以下這些view的autoresizingMask默認(rèn)就是18(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)
- 1.從xib里面創(chuàng)建出來的默認(rèn)控件(Xcode自動(dòng)創(chuàng)建出來的那個(gè)view)
- 2.控制器的view
如果不希望控件擁有autoresizingMask的自動(dòng)伸縮功能,應(yīng)該設(shè)置為none
- ViewController
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
/*
一般情況下,以下這些view的autoresizingMask默認(rèn)就是18(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)
1.從xib里面創(chuàng)建出來的默認(rèn)控件(Xcode自動(dòng)創(chuàng)建出來的那個(gè)view)
2.控制器的view
如果不希望控件擁有autoresizingMask的自動(dòng)伸縮功能,應(yīng)該設(shè)置為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;
// 去掉默認(rèn)的自動(dòng)拉伸寬高功能
// 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;
}
@end

顯示效果圖片:

