繁体   English   中英

UITabBarController显示

[英]UITabBarController display

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

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

以下是我为UITabBarController设置框架的代码行:

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

放置这行代码并进行检查,您必须相应地设置框架。

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(不设置框架)!

最简单,最干净的技术是:

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应用程序”模板创建新项目,并查看Apple的工作方式

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

这是由于早期的iPhone和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)];
}

或者,您可以设置“自动调整大小蒙版”以将视图自动调整为新的屏幕尺寸或方向(自动调整大小的用处取决于视图的定义方式)。

[rootTabBarController.view setAutoResizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

您需要根据设备分辨率调整框架。 我们知道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 )

然后将框架设置为

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

希望对您有帮助。

暂无
暂无

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

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