简体   繁体   中英

UITabBarController display

I am newbie in iOS development. I am developing an application that is using tab bar controller. I am setting frame for tab bar controller programmatically but when I switch to iPhone 5 there is white space created between tab bar items and the main view. following is screen shot of application on iPhone 5 simulator.

4.5视网膜显示模拟器上的屏幕截图

Following is a line of code where I am setting frame for UITabBarController :

[rootTabBarController.view setFrame:CGRectMake(0,-20,320, 480)];

put this line of code and check it,you have to set frames accordingly.

if ([[UIScreen mainScreen] bounds].size.height == 568)
     {

       [rootTabBarController.view setFrame:CGRectMake(0,0,320, 568)];
     }
 else
     {
        [rootTabBarController.view setFrame:CGRectMake(0,0,320, 480)];
     }

just.dont.do.it (do not set frame) !!!

The most simple and clean technique is:

YourAppDelegate.h:

@property(nonatomic,retain) UITabBarController * tabBarController;

YourAppDelegate.m:

@synthesize tabBarController;
#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

self.tabBarController = [[[UITabBarController alloc] init] autorelease];

UINavigationController      *viewController1 = [[[UINavigationController alloc] initWithRootViewController:[[[UIViewController alloc] init] autorelease]] autorelease];

UINavigationController      *viewController2 = [[[UINavigationController alloc] initWithRootViewController:[[[UIViewController alloc] init] autorelease]] autorelease];

self.tabBarController.viewControllers = @[viewController1, viewController2];
self.tabBarController.customizableViewControllers = nil;

self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];


return YES;

}

And everything will be OK. Frames, rotation, autoresizing, on iPhone, iPad, etc etc.

Btw, you can create the new project with "Tab Bar application" template in Xcode and see how Apple does it

and .. (advice, that can help you in future) UITabBarController must be on the top of view hierarchy (directly on the UIWindow) for correct rotating and etc, my sample code includes it (it can save you some time in future)

This is because of a height difference between the earlier iPhones and the iPhone 5.

You can solve this in two ways:

Set the frame size manually when you're running on a iPhone 5.

BOOL isIphone5 = (([[UIDevice currentDevice] userInterfaceIdiom] 
== UIUserInterfaceIdiomPhone) && (([UIScreen mainScreen].bounds.size.height) >= 568));
if(isIphone5)
{
   [rootTabBarController.view setFrame:CGRectMake(0,0,320, 568)];
}
else{
   [rootTabBarController.view setFrame:CGRectMake(0,0,320, 480)];
}

Or you can set AutoResizingmasks to have your view auto-resize to new screensizes or orientations (The usefulness of auto-resizing depends on how the views are defined).

[rootTabBarController.view setAutoResizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

You need to adjust frame according to the device resolution. As we know iPhone 5 screen size is (320,568) so you can check whether you are using iPhone 5 (4 inch screen) or other (3.5 inch screen) using

#define IS_IPHONE ( [[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define IS_IPHONE_5 ( IS_IPHONE && IS_HEIGHT_GTE_568 )

and then set frame as

[rootTabBarController.view setFrame:CGRectMake(0,-20,320,IS_IPHONE_5?568.0f:480.0f)];

Hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM