简体   繁体   English

自动调整大小不适用于以编程方式添加的UIView

[英]Autoresizing not working for UIView added programatically

I have followed this SO link for autoresizing , but Autoresizing not working. 我已经按照此SO链接进行了自动调整大小 ,但是自动调整大小不起作用。
How to set frame programmatically with autoresizing? 如何通过自动调整大小以编程方式设置框架?

I have set frame for iPhone 4 我已经为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];

But it is not working in iPad or iPhone 6 但它不适用于iPad或iPhone 6

First thing first : As per open suggestion, you should use constraints. 首先,根据公开建议,您应该使用约束。

Add autoresizing mask after adding it to view. 将自动调整大小的蒙版添加到视图后,添加它。

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];

If I understand you question correctly you're trying to set red view on top with height = 120 如果我理解您的问题正确,那么您正在尝试将红色视图设置为高度= 120

ADD EXPLANATION 添加说明

You could achieve it with using constraints: 您可以使用约束来实现:

        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]];

Apple Documentation 苹果资料

You are setting a UIView with a frame (0, 0, 320, 120) . 您正在使用框架(0, 0, 320, 120) 0,0,320,120)设置UIView。 This frame will fit the iPhone 4 screen, as the phone screen width is 320 pixels. 该框架将适合iPhone 4屏幕,因为手机屏幕宽度为320像素。 But you cant expect same when you run the code in iPhone 6/6s. 但是当您在iPhone 6 / 6s中运行代码时,您无法期望得到相同的结果。 Setting Autoresizing will not handle this. 设置自动调整大小将无法解决此问题。 You need to use constraints/autolayout for that. 您需要为此使用约束/自动布局。

Autoresizing masks describe how a subview will resize or move when its superview is resized.

So after adding this view, if you change the phone orientation, this will resize the view in position accordingly. 因此,添加此视图后,如果更改手机方向,则会相应地调整视图的大小。 But you need to set the frame according to the superview first. 但是您需要首先根据超级视图设置框架。 You can set the width dynamically, like: (0, 0, self.view.frame.size.width, 120) . 您可以动态设置宽度,例如: (0, 0, self.view.frame.size.width, 120)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM