Why am I having to manually set my view's frame in viewDidLoad?
I have a pretty basic setup with a UINavigationController inside a UITabBarController. I'm wanting to programmatically layout the view of the rootViewController of that navcontroller, but when I look at self.view.frame inside viewDidLoad, I get this (in landscape, for example):
1. view frame: {{20, 0}, {748, 1024}} // looks like an odd portrait mode
Then I autorotate to Portrait, and I get this:
2. view frame: {{0, 0}, {768, 911}}
Then when I go back to Landscape, the frame is now this:
3. view frame: {{0, 0}, {1024, 655}}
And further autorotation events will flip-flop b开发者_如何学编程etween frame values #2 & #3.
To get around the weirdness of #1, I'm currently doing this in viewDidLoad:
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
self.view.frame = CGRectMake(0, 0, 768, 911);
} else {
self.view.frame = CGRectMake(0, 0, 1024, 655);
}
I feel like I'm obviously missing something here. Why would the default frame of the view not match the frame when it autorotates back to the same orientation? Does the view frame not get set to the initial orientation? Very confused...
I should mention that none of the above, including my kludgy hack, changes anything visually. The reason I have the hack is so that when I layout my subviews into this view, they'll be based off of where I expect them to be, which is the top left corner just under the navigation bar.
What am I doing wrong?
UPDATE: turning off all the autosizing stuff on the view changes result #1 to be:
view frame: {{0, 0}, {748, 1024}}
That seems a tiny bit closer, but still not matching #3.
The frame is not guaranteed to be the same in viewDidLoad as it will be when the view is eventually displayed. UIKit adjusts the frame of your view controller's view prior to displaying it, based on the context in which will appear. The size is determined based on interface orientation and the dimensions of any visible navigation bar, tab bar, toolbar, or status bar (which itself has a height that can change, e.g. when you're on a phone call).
It helps to understand what happens when a view controller's view is loaded and displayed:
Something accesses your view controller's
view
property for the first time. This may occur in your own code, or in UIKit in response to a user action like selecting a tab.UIKit lazy-loads your view controller's view by calling
loadView
if it's defined, or by loading the view from the NIB that was specified ininitWithNibName:bundle:
. If neither exists, UIKit just loads an empty view.UIKit calls
viewDidLoad
once the view and its subviews have been fully loaded. At this point the view's frame will be whatever it was set to in the NIB, or inloadView
.Something calls for UIKit to display your view controller's view. Again, this may be a user action like tapping on a tab, or an explicit method call in your code like
pushViewController:animated:
orpresentModalViewController:animated:
.UIKit resizes the view based on the context in which it will be presented, as described above.
UIKit calls
viewWillAppear:
. The frame should now be the size that will be displayed. (?) EDIT: This may no longer be true. See the comments below.UIKit displays the view, with or without animations.
UIKit calls
viewDidAppear:
.
As you can see, if you need to know the size of your view's frame before it gets presented, viewWillAppear:
is your one and only opportunity. Just remember that this size may change after the view appears for various reasons, including rotation events or changes in status bar height. For this reason, it's important to give every subview an appropriate autoresizingMask, to ensure that the layout can adjust itself properly for any change in bounds.
If you wish to build your view hierarchy manually, the recommended place to do so is in loadView. Since you construct the view yourself in this method, you can initialize its frame to whatever you'd like. The size you choose doesn't matter much, since UIKit is likely to change it on you anyway. Just make sure you set your autoresizingMasks appropriately.
There will be a method in your class
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations return YES; }
Put your frame in this method... Then it will work fine.
For example:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if(interfaceOrientation == UIDeviceOrientationPortrait || UIDeviceOrientationPortraitUpsideDown)
self.view.frame = CGRectMake(0, 0, FRAME.WIDTHinPOTRAIT, FRAME.HEIGHTinPOTRAIT);
else if(interfaceOrientation == UIDeviceOrientationLandscapeRight || UIDeviceOrientationLandscapeLeft)
self.view.frame = CGRectMake(0, 0, FRAME.WIDTHinLANDSCAPE, FRAME.HEIGHTinLANDSCAPE);
return YES;
}
FRAME.WIDTH > desired width FRAME.HEIGHT > desired height
精彩评论