开发者

iPad/iPod: autoRotate resizes view in window

开发者 https://www.devze.com 2023-01-01 17:19 出处:网络
Developing an iPad interface, I have a scenario where I have a UIViewController which manages a view that gets placed directly as a subview of the main UIWindow.

Developing an iPad interface, I have a scenario where I have a UIViewController which manages a view that gets placed directly as a subview of the main UIWindow.

Before being placed in the UIWindow, that view gets resized to a non-standard size, let's say, 768x460, and positioned at the bottom of the screen.

When rotating the device, the autoRotate feature of the UIViewController causes the view to be resized so that it fills the entire UIWindow space.

I thought this might be because in the XIB, the view is s开发者_JAVA技巧et to window size, but when I changed it to reflect the desired size, it still expanded it to the window size.

Then I went into the MainWindow XIB and turned off autoresizeSubviews, and it still happens.

This is a very frustrating problem, I am hoping that there is merely something obvious that I am missing out on.

Anyone have any bright ideas?

Code by request:

browseController = [[BrowseController alloc] initWithNibName:@"BrowseController" bundle:nil];

[[browseController view] setFrame:CGRectMake(0, 544, 768, 460)];

[window addSubview:[browseController view]];


IF you are in interface builder, click on the size tab when you are editing the view. Under "autosizing" click on all the red arrows so that they disappear. You do not want any red arrows there. Or in code you can do the following:

browseController.autoresizingMask = UIViewAutoresizingNone;


What about springs and struts? (aka autoresizingMask) See here. In code you might need the extra line

[[browseController view] setAutoresizingMask:UIViewAutoresizingNone];


Not enough info from your question, but this is usually the problem (so I'm making an educated guess) :

Apple states that only ONE UIView should exist in the UIWindow at root level - if there's more than one, a lot of behaviour is undefined. Often there's an extra one you didn't notice was there / forgot about from weeks ago.

In practice, I've found that when there's "more than one", a lot of these rotation bugs occur, where Apple's code gets confused, and starts randomly re-positioning things.

Standard workaround:

  1. In every app you ever make
  2. ...always have a single, empty, "MyRootViewController" which you add to the UIWindow
  3. ...then add as many views as you want as subviews inside it

(because Apple's strange bugs only affect the UIWindow itself - all subviews work "as expected")


I just had this problem, and here's how I fixed it:

Make sure you've defined a view controller for the view that you're attaching to the top level of the window. If you don't, layout will go crazy on you.

0

精彩评论

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