[英]Autoresizing not working for UIView added programatically
我已经按照此SO链接进行了自动调整大小 ,但是自动调整大小不起作用。
如何通过自动调整大小以编程方式设置框架?
我已经为iPhone 4设置了框架
UIView *box = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 120)];
[box setBackgroundColor:[UIColor redColor]];
[box setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[box setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[box setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[box setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[box setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
[self.view addSubview:box];
但它不适用于iPad或iPhone 6
首先,根据公开建议,您应该使用约束。
将自动调整大小的蒙版添加到视图后,添加它。
UIView *box = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 120)];
[box setBackgroundColor:[UIColor redColor]];
[self.view addSubview:box];
[box setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[box setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[box setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[box setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[box setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
如果我理解您的问题正确,那么您正在尝试将红色视图设置为高度= 120
添加说明
您可以使用约束来实现:
UIView *box = [[UIView alloc] init];
// Prevent creating constraints from masks automatically
box.translatesAutoresizingMaskIntoConstraints = NO;
box.backgroundColor = [UIColor redColor];
[self.view addSubview:box];
// Define metrics (constants) which will be used to create constraints.
// Key @"boxSize" - name which will be used in constraints, Value - constant
NSDictionary *metrics = @{@"boxSize" : @(120)};
// Define views that will participate in auto layout constraints.
// Key @"readBox" - name which will be used in constraints, Value - real UIView object
NSDictionary *views = @{ @"redBox" : box };
// Here we create constraints. For Vertical, and for Horizontal
// I'm using Visual language format (you can find it in Apple Documentation
// In a few words:
// H:|-0-[redBox]-0-|
// "H" - means horizontal
// "|" - short cut for parent view (in our case it is UIViewController.view)
// "[redBox]" - view name from view's dictionary
// "-0-" - gap between views (you could set number), in our case it is "|" and "[redBox]"
// "[redBox(boxSize)]" - means that view (redBox) size should be qual to "boxSize" from metrics' dictionary
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[redBox]-0-|" options:NSLayoutFormatAlignAllCenterY metrics:metrics views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[redBox(boxSize)]" options:NSLayoutFormatAlignAllCenterY metrics:metrics views:views]];
您正在使用框架(0, 0, 320, 120)
0,0,320,120)设置UIView。 该框架将适合iPhone 4屏幕,因为手机屏幕宽度为320像素。 但是当您在iPhone 6 / 6s中运行代码时,您无法期望得到相同的结果。 设置自动调整大小将无法解决此问题。 您需要为此使用约束/自动布局。
Autoresizing masks describe how a subview will resize or move when its superview is resized.
因此,添加此视图后,如果更改手机方向,则会相应地调整视图的大小。 但是您需要首先根据超级视图设置框架。 您可以动态设置宽度,例如: (0, 0, self.view.frame.size.width, 120)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.