问题
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.
回答1:
from
http://stackoverflow.com/q/7754851/4030971
Setting only (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) is equivalent to:
Setting (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin) in code is equivalent to:
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.
回答2:
You're overwriting all of your resizing masks.
You can do it like this:
[box setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
回答3:
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;
回答4:
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.
来源:https://stackoverflow.com/questions/28804126/autoresizing-not-working-programmatically