开发者

self.view.frame changed size ifself unexpectedly in landscape mode

I've setup the view controller's view via IB. The view is set to landscape with width 480 and height 320. It loaded correctly in the simulator. However, when I checked self.frame.size by using NSLog, I found that the frame size changed itself without any rotation of device or any code. Here is what I get:

In v开发者_C百科iewDidLoad --> size = 480 x 320

In viewWillAppear --> size = 480 x 320

In viewDidAppear --> size = 320 x 480

I only have codes in viewDidLoad, but not viewWillAppear nor viewDidAppear. Anyone knows what happened behind the scene? Thanks.


Try something like this:

NSLog("frame=%@ transform=%@", NSStringFromCGRect(self.frame), NSStringFromCGAffineTransform(self.transform));

I'm willing to bet that transform is the identity (1,0,0,1,0,0) when frame size is 480x320, and a 90° rotation (0,1,-1,0,0,0 or 0,-1,1,0,0,0) when frame size is 320x480.

What's going on?

Well, viewDidLoad (and I think viewWillAppear) are called before the view is added to the view hierarchy. When it's added to the hierarchy, UIKit also sets the view transform, which is how rotation happens.

But why does that change the frame?

Well, the "frame" is relative to the parent view, which is the UIWindow. The window doesn't rotate; the view controller's view does. From the window's perspective, everything is effectively in portrait mode.

You probably want self.view.bounds.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜