简体   繁体   中英

Autoresizing not working for UIView added programatically

I have followed this SO link for autoresizing , but Autoresizing not working.
How to set frame programmatically with autoresizing?

I have set frame for 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

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

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) . This frame will fit the iPhone 4 screen, as the phone screen width is 320 pixels. But you cant expect same when you run the code in 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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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