简体   繁体   English

UITabBarController显示

[英]UITabBarController display

I am newbie in iOS development. 我是iOS开发的新手。 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. 我正在以编程方式设置选项卡栏控制器的框架,但是当我切换到iPhone 5时,选项卡栏项和主视图之间会创建空白。 following is screen shot of application on iPhone 5 simulator. 以下是iPhone 5模拟器上的应用程序的屏幕截图。

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

Following is a line of code where I am setting frame for UITabBarController : 以下是我为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) !!! just.dont.do.it(不设置框架)!

The most simple and clean technique is: 最简单,最干净的技术是:

YourAppDelegate.h: YourAppDelegate.h:

@property(nonatomic,retain) UITabBarController * tabBarController;

YourAppDelegate.m: 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. 框架,旋转,自动调整大小,在iPhone,iPad等上

Btw, you can create the new project with "Tab Bar application" template in Xcode and see how Apple does it 顺便说一句,您可以使用Xcode中的“ Tab Bar应用程序”模板创建新项目,并查看Apple的工作方式

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) 和..(建议,将来可以为您提供帮助)UITabBarController必须位于视图层次结构的顶部(直接位于UIWindow上)以正确旋转等,我的示例代码包括了它(可以为您节省一些时间)

This is because of a height difference between the earlier iPhones and the iPhone 5. 这是由于早期的iPhone和iPhone 5之间的高度差异。

You can solve this in two ways: 您可以通过两种方式解决此问题:

Set the frame size manually when you're running on a iPhone 5. 在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 我们知道iPhone 5的屏幕尺寸为(320,568),因此您可以使用以下方法检查是否正在使用iPhone 5(4英寸屏幕)或其他(3.5英寸屏幕)

#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. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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