1

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.

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Malav Soni
  • 2,739
  • 1
  • 23
  • 52
  • More info here: [UIView autoresizingMask - Interface Builder to Code - Programmatically create struts and springs](http://stackoverflow.com/questions/10468389/uiview-autoresizingmask-interface-builder-to-code-programmatically-create-st) – Aaron Brager Mar 02 '15 at 06:59

4 Answers4

3

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.

Mahesh Agrawal
  • 3,348
  • 20
  • 34
  • its iphone 6 simulator. you are not using auto layout. you need to check self.view.frame.size.width and instead of 320 use the width of current view width. – Mahesh Agrawal Mar 02 '15 at 07:43
  • Okey... The scenario is I create the box using interface builder and apply autosizing. It work perfect in all screen size. In this we define frame and autosizing also... Now i want the same thing to happen programmatically. where i define frame and autosizing – Malav Soni Mar 02 '15 at 07:47
  • you can check above comment – Malav Soni Mar 02 '15 at 07:49
  • and yes AUTO LAYOUT Is DISABLE – Malav Soni Mar 02 '15 at 07:51
  • if you want to add a view which will aligned to bottom and take full width of the main screen then you need to set the width of the frame same as your main screen and the flexible width is not required. no resizing will be required. in case of bottom align you need to just set the bottom margin. – Mahesh Agrawal Mar 02 '15 at 07:55
  • UIView *box = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-120, self.view.frame.size.width, 120)]; [box setBackgroundColor:[UIColor redColor]]; no need of any sizing. – Mahesh Agrawal Mar 02 '15 at 08:04
0

You're overwriting all of your resizing masks.

You can do it like this:

[box setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
0

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;
oren
  • 3,159
  • 18
  • 33
  • Can you describe what do you see? I did a simple test and it worked for me. – oren Mar 02 '15 at 07:02
  • https://drive.google.com/file/d/0Byb4VeRBzTKvY0ZqOEpOOEh6WWM/view?usp=sharing You can check out this screen shot... – Malav Soni Mar 02 '15 at 07:09
  • Ok, so it's because iPhone6 has bigger screen size than your box frame. See my edit answer – oren Mar 02 '15 at 07:15
  • That's correct... but i would like to user autoresizing as it is working through interface builder but not programmatically .. – Malav Soni Mar 02 '15 at 07:43
  • What do you mean? Did you see my edit in the answer? you have to set the width and origin y as your screen size... – oren Mar 02 '15 at 10:13
  • I know i can implement this way as you wrote... but autoresizing can also do this from interface builder.... I would like to use autoresize programmatically instead of setting frame. I am clearing my concepts for autoresizing ... that's why i would like to use autoresizing through coding. – Malav Soni Mar 02 '15 at 11:25
0

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.

Malav Soni
  • 2,739
  • 1
  • 23
  • 52