我是iOS开发的新手。我正在开发一个使用标签栏控制器的应用程序。我正在以编程方式设置选项卡栏控制器的框架,但是当我切换到iPhone 5时,选项卡栏项目和主视图之间会产生空白。以下是iPhone 5模拟器上的应用程序的屏幕截图。

下面是我为UITabBarController设置框架的代码行
[rootTabBarController.view setFrame:CGRectMake(0,-20,320, 480)];发布于 2013-04-25 20:32:56
放入这行代码并检查它,您必须相应地设置框架。
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
[rootTabBarController.view setFrame:CGRectMake(0,0,320, 568)];
}
else
{
[rootTabBarController.view setFrame:CGRectMake(0,0,320, 480)];
}发布于 2013-04-25 20:35:08
just.dont.do.it (不设置帧)!
最简单、最干净的技术是:
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;}
一切都会好起来的。框架,旋转,自动调整大小,在iPhone,iPad等。
顺便说一句,你可以在Xcode中用"Tab Bar application“模板创建一个新项目,看看Apple是怎么做的
然后..。(建议,将来可以帮助你) UITabBarController必须位于视图层次结构的顶部(直接在UIWindow上)才能正确旋转等,我的示例代码包含了它(它可以为你节省一些将来的时间)
发布于 2013-04-25 20:37:26
这是因为早期的iPhones和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)];
}或者,您可以将AutoResizingmasks设置为让视图自动调整大小以适应新的屏幕大小或方向(自动调整大小的作用取决于视图的定义)。
[rootTabBarController.view setAutoResizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];https://stackoverflow.com/questions/16214819
复制相似问题