开发者

Autosizing doesn't work when loading from XIB after rotating

开发者 https://www.devze.com 2023-01-29 13:22 出处:网络
In my app I add a view loaded from XIB with UIViewAutosizingMaskFlexibleWidth and have the desired result only in one of two cases:

In my app I add a view loaded from XIB with UIViewAutosizingMaskFlexibleWidth and have the desired result only in one of two cases:

  1. Adding subview on portrait, and rotating to landscape, resizes the subview correctly;
  2. Adding subview on landscape do not cause the subview to resize its width the way it does when rotating;

Here is the code:

viewMenuList = [[MenuListController alloc] initWithNibName:@"MenuListController" bundle:[NSBundle mainBundle]];
viewMenuList.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.view.autoresizesSubviews = YES;
[self.view addSubview:viewMenuList.view];
[self.view sendSubviewToBack:viewMenuList.view];
[viewMenuList.view setNeedsLayout];
[viewMenuList.view setNeedsDisplay];
NSLog(@"frame do menu links view %f, %f, %f, %f", self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
NSLog(@"frame do menu list view %f, %f, %f, %f", viewMenuList.view.frame.origin.x, viewMenuList.view.frame.origin.y, viewMenuList.view.frame.size.width, viewMenuList.view.frame.size.height);

The output in the console is:

2010-12-07 16:34:22.552 SlideShow_iPad[19352:207] frame do 开发者_开发百科menu links view 0.000000, 0.000000, 1030.000000, 111.000000
2010-12-07 16:34:22.596 SlideShow_iPad[19352:207] frame do menu list view 0.000000, 0.000000, 768.000000, 109.000000

The last line should display an width of 1030, shouldnt it? It does resize to 1030 after rotating to portrait and going back to landscape.

The strangest thing is that I do use the same technic in the superview and worked.


You shouldn't mix both Interface Builder and Objective-C code to handle resizing. Remove autoresizingMask and autoresizesSubviews from code and do both only in Interface Builder. You might have conflicting defines now.

autoresizingMark: command-3 to view size properties. Check "Autosizing" part, the animation shows pretty nicely how your resizing will look.

autoresizesSubviews: command-1 to view drawing properties. There's a checkbox for this.

0

精彩评论

暂无评论...
验证码 换一张
取 消