026自动调整视图的子元素

效果如下:

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UIViewController
4 @property (nonatomic, strong) UILabel *lblParent;
5 @property (nonatomic, strong) UILabel *lblChild;
6 
7 @end

ViewController.m

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 - (void)resizeDidPush;
  5 - (void)leftDidSwitch:(id)sender;
  6 - (void)rightDidSwitch:(id)sender;
  7 - (void)topDidSwitch:(id)sender;
  8 - (void)bottomDidSwitch:(id)sender;
  9 - (void)widthDidSwitch:(id)sender;
 10 - (void)heightDidSwitch:(id)sender;
 11 - (void)addSwitch:(NSString *)caption frame:(CGRect)frame action:(SEL)action;
 12 @end
 13 
 14 @implementation ViewController
 15 
 16 - (void)viewDidLoad {
 17     [super viewDidLoad];
 18     _lblParent = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 260, 220)];
 19     _lblParent.text = @"Parent";
 20     _lblParent.textAlignment = NSTextAlignmentCenter;
 21     _lblParent.textColor = [UIColor whiteColor];
 22     _lblParent.backgroundColor = [UIColor orangeColor];
 23     _lblParent.layer.masksToBounds = YES;
 24     _lblParent.layer.cornerRadius = 10.0;
 25     [self.view addSubview:_lblParent];
 26     
 27     _lblChild = [[UILabel alloc] initWithFrame:CGRectInset(_lblParent.bounds, 30, 30)]; //width=260-30*2=200, height = 220-30*2=160
 28     _lblChild.text = @"Child";
 29     _lblChild.textAlignment = NSTextAlignmentCenter;
 30     _lblChild.textColor = [UIColor whiteColor];
 31     _lblChild.backgroundColor = [UIColor brownColor];
 32     [_lblParent addSubview:_lblChild];
 33     
 34     //追加按钮btnResize
 35     UIButton *btnResize = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 36     btnResize.frame = CGRectMake(0, 0, 100, 40);
 37     CGPoint newPoint = self.view.center;
 38     newPoint.y = self.view.frame.size.height - 40;
 39     btnResize.center = newPoint;
 40     btnResize.backgroundColor = [UIColor lightGrayColor];
 41     btnResize.layer.masksToBounds = YES;
 42     btnResize.layer.cornerRadius = 8.0;
 43     [btnResize setTitle:@"Resize" forState:UIControlStateNormal];
 44     [btnResize setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
 45     [btnResize addTarget:self action:@selector(resizeDidPush) forControlEvents:UIControlEventTouchUpInside];
 46     [self.view addSubview:btnResize];
 47     
 48     //追加开关按钮
 49     CGRect lblRect = CGRectMake(20, 320, 250, 30);
 50     [self addSwitch:@"FlexibleLeftMargin" frame:lblRect action:@selector(leftDidSwitch:)];
 51     
 52     lblRect.origin = CGPointMake(20, lblRect.origin.y + 35);
 53     [self addSwitch:@"FlexibleRightMargin" frame:lblRect action:@selector(rightDidSwitch:)];
 54     
 55     lblRect.origin = CGPointMake(20, lblRect.origin.y + 35);
 56     [self addSwitch:@"FlexibleTopMargin" frame:lblRect action:@selector(topDidSwitch:)];
 57     
 58     lblRect.origin = CGPointMake(20, lblRect.origin.y + 35);
 59     [self addSwitch:@"FlexibleBottomMargin" frame:lblRect action:@selector(bottomDidSwitch:)];
 60     
 61     lblRect.origin = CGPointMake(20, lblRect.origin.y + 35);
 62     [self addSwitch:@"FlexibleWidth" frame:lblRect action:@selector(widthDidSwitch:)];
 63     
 64     lblRect.origin = CGPointMake(20, lblRect.origin.y + 35);
 65     [self addSwitch:@"FlexibleHeight" frame:lblRect action:@selector(heightDidSwitch:)];
 66 }
 67 
 68 - (void)didReceiveMemoryWarning {
 69     [super didReceiveMemoryWarning];
 70     // Dispose of any resources that can be recreated.
 71 }
 72 
 73 #pragma mark - Private Methods
 74 - (void)resizeDidPush {
 75     if (_lblParent.frame.size.width == 260) {
 76         _lblParent.frame = CGRectMake(20, 20, 300, 230);
 77     } else {
 78         _lblParent.frame = CGRectMake(20, 20, 260, 220);
 79     }
 80 }
 81 
 82 - (void)leftDidSwitch:(id)sender {
 83     //如果视图的autoresizesSubviews属性声明被设置为YES(默认值),则其子视图会根据autoresizingMask属性的值自动进行尺寸调整。简单配置一下视图的自动尺寸调整掩码常常就能使应用程序得到合适的行为;否则,应用程序就必须通过重载layoutSubviews方法来提供自己的实现
 84     //视图的左边距将随着父视图宽度的变化而按比例进行调整
 85     if ([sender isOn]) {
 86         _lblChild.autoresizingMask |= UIViewAutoresizingFlexibleLeftMargin;
 87     } else {
 88         _lblChild.autoresizingMask &= ~UIViewAutoresizingFlexibleLeftMargin;
 89     }
 90 }
 91 
 92 - (void)rightDidSwitch:(id)sender {
 93     //视图的右边距将随着父视图宽度的变化而按比例进行调整
 94     if ([sender isOn]) {
 95         _lblChild.autoresizingMask |= UIViewAutoresizingFlexibleRightMargin;
 96     } else {
 97         _lblChild.autoresizingMask &= ~UIViewAutoresizingFlexibleRightMargin;
 98     }
 99 }
100 
101 - (void)topDidSwitch:(id)sender {
102     //视图的上边距将随着父视图高度的变化而按比例进行调整
103     if ([sender isOn]) {
104         _lblChild.autoresizingMask |= UIViewAutoresizingFlexibleTopMargin;
105     } else {
106         _lblChild.autoresizingMask &= ~UIViewAutoresizingFlexibleTopMargin;
107     }
108 }
109 
110 - (void)bottomDidSwitch:(id)sender {
111     //视图的下边距将随着父视图高度的变化而按比例进行调整
112     if ([sender isOn]) {
113         _lblChild.autoresizingMask |= UIViewAutoresizingFlexibleBottomMargin;
114     } else {
115         _lblChild.autoresizingMask &= ~UIViewAutoresizingFlexibleBottomMargin;
116     }
117 }
118 
119 - (void)widthDidSwitch:(id)sender {
120     //视图的宽度将和父视图的宽度一起成比例变化
121     if ([sender isOn]) {
122         _lblChild.autoresizingMask |= UIViewAutoresizingFlexibleWidth;
123     } else {
124         _lblChild.autoresizingMask &= ~UIViewAutoresizingFlexibleWidth;
125     }
126 }
127 
128 - (void)heightDidSwitch:(id)sender {
129     //视图的高度将和父视图的高度一起成比例变化
130     if ([sender isOn]) {
131         _lblChild.autoresizingMask |= UIViewAutoresizingFlexibleHeight;
132     } else {
133         _lblChild.autoresizingMask &= ~UIViewAutoresizingFlexibleHeight;
134     }
135 }
136 
137 - (void)addSwitch:(NSString *)caption frame:(CGRect)frame action:(SEL)action {
138     UILabel *lblCaption = [[UILabel alloc] initWithFrame:frame];
139     lblCaption.text = caption;
140     [self.view addSubview:lblCaption];
141     
142     UISwitch *swtResize = [[UISwitch alloc] initWithFrame:CGRectZero];
143     CGPoint newPoint = lblCaption.center;
144     newPoint.x += 110;
145     swtResize.center = newPoint;
146     [swtResize addTarget:self action:action forControlEvents:UIControlEventValueChanged];
147     [self.view addSubview:swtResize];
148 }
149 
150 @end
原文地址:https://www.cnblogs.com/huangjianwu/p/4575990.html