Malav Soni
Malav Soni

Reputation: 2838

Autoresizing Not Working Programmatically

I want to create a UIView which will resize it's width according to iPhone Screen size and it should be remain at bottom of view.

I created fresh project with storyboard and add below code.

Here is my code

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:true];

    UIView *box = [[UIView alloc]initWithFrame:CGRectMake(0, 448, 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];
}

Auto layout is OFF.

If I do same thing by apply autoresizing from interface builder then it's working perfectly.

I didn't understand any reason for why it's not working.

Upvotes: 1

Views: 1859

Answers (4)

Malav Soni
Malav Soni

Reputation: 2838

Little mistake from my side. My code is correct.

The issue is related to xCode 6.1, in launch screen we have xib file from xCode 6 so i have to remove reference of it from General Setting-> App Icon and Launch Images and use Assert Catalog instead of launchscreen.xib and it start working....

Anyway thanks for reply to my question.

Upvotes: 0

oren
oren

Reputation: 3199

It's a mask, try do this:

[box setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth |
 UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
  • also, if you want the height wont change, remove UIViewAutoresizingFlexibleHeight

edit

To set it for any device size do something like this:

CGRect fr = box.frame;
fr.size.width = self.view.frame.size.width;
fr.origin.y = self.view.frame.size.height - 120;
box.frame = fr;

Upvotes: 0

Mahesh Agrawal
Mahesh Agrawal

Reputation: 3358

from

http://stackoverflow.com/q/7754851/4030971 

Setting only (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) is equivalent to:

enter image description here

Setting (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin) in code is equivalent to:

enter image description here

i think -

you need to set only flexible width and flexible bottom hence its effecting reverse. don't use flexible height. if you need to use flexible height then instead of assigning flexible top with flexible width assign flexible width, flexible height and flexible bottom. so it will remove top one. see assigning left right top bottom removing the corresponding if flexible width and flexible height is assigned. now you should do according. i think this will help.

Upvotes: 3

Aaron Brager
Aaron Brager

Reputation: 66234

You're overwriting all of your resizing masks.

You can do it like this:

[box setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];

Upvotes: 0

Related Questions