I made a complete program that reproduces the problem in my real program. Basically, I nest a button in a navigation controller, and I need to resize it when the device rotates.
The goal is to have the button touch the edges of the user area inside the nav controller in all orientations.
Initially, I don't see the button's bottom edge. Its side borders end about 0 pixels above the bottom of the screen.
I rotate to landscape, and I don't see any of the edges at all.
I rotate back to portrait, and I can't see the whole right half of the button.
WTF?
(For why I resize nav.view, see here: http://arstechnica.com/civis/viewtopic.php?f=20&t=1128641 It's the update for post #4. I tried to resize only the height of nav.view, but I couldn't see scrollbars in scrolling views, apparently because they were outside the screen. This was even after I resized the scrollview itself.)
The goal is to have the button touch the edges of the user area inside the nav controller in all orientations.
Initially, I don't see the button's bottom edge. Its side borders end about 0 pixels above the bottom of the screen.
I rotate to landscape, and I don't see any of the edges at all.
I rotate back to portrait, and I can't see the whole right half of the button.
WTF?
(For why I resize nav.view, see here: http://arstechnica.com/civis/viewtopic.php?f=20&t=1128641 It's the update for post #4. I tried to resize only the height of nav.view, but I couldn't see scrollbars in scrolling views, apparently because they were outside the screen. This was even after I resized the scrollview itself.)
Code:
@interface TestProjectAppDelegate : UIViewController <UIApplicationDelegate>
{
CGSize sizeOfView;
UIWindow *window;
UINavigationController* nav;
UIButton* b;
UIViewController* vc;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
#import "TestProjectAppDelegate.h"
#import "Globals.h"
@implementation TestProjectAppDelegate
@synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[b setTitle:@"Test" forState:UIControlStateNormal];
vc = [UIViewController new];
[vc.view addSubview:b];
nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.view = [UIView new];
[self.view addSubview:nav.view];
[window addSubview:self.view];
[window makeKeyAndVisible];
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
CGFloat w = window.frame.size.width;
CGFloat h = window.frame.size.height;
CGSize sz = CGSizeZero;
switch (interfaceOrientation) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
sz = CGSizeMake(MIN(w, h), MAX(w, h));
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
sz = CGSizeMake(MAX(w, h), MIN(w, h));
break;
}
if (CGSizeEqualToSize(sz, sizeOfView))
{
return YES;
}
CGRect r = CGRectZero;
r.size = sz;
self.view.frame = r;
nav.view.frame = r;
r.size.height -= nav.navigationBar.frame.size.height;
self.view.frame = r;
b.frame = r;
return YES;
}
- (void) viewDidAppear: (BOOL) animated
{
//manually adjust the frame of the main view to prevent it from appearing under the status bar. (From Stack Overflow post.)
UIApplication *app = [UIApplication sharedApplication];
if(!app.statusBarHidden)
{
[self.view setFrame:CGRectMake(0.0,app.statusBarFrame.size.height, self.view.bounds.size.width, self.view.bounds.size.height - app.statusBarFrame.size.height)];
}
//corrects for the nav bar appearing 20 pixels too low for no damn reason.
CGRect r = nav.navigationBar.frame;
r.origin = CGPointZero;
nav.navigationBar.frame = r;
}
- (void)dealloc
{
[window release];
[super dealloc];
}
@end