【问题标题】:UITabBarController displayUITabBarController 显示
【发布时间】:2013-04-25 12:29:22
【问题描述】:

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

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

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

【问题讨论】:

  • 您必须更改 iphone5 的框架,因为 iphone 5 的屏幕尺寸比 iphone 4 更大。

标签: ios objective-c uitabbarcontroller cgrectmake


【解决方案1】:

把这行代码检查一下,你必须相应地设置框架。

 

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

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

【讨论】:

  • 我尝试了你的解决方案,但是当我在编码中使用它时,标签栏会离开屏幕......
  • 背景图片或标签栏有问题吗?
  • 由于标签栏,我遇到了背景图像的问题
【解决方案2】:

这是因为早期 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)];
}

或者您可以设置 AutoResizingmasks 让您的视图自动调整为新的屏幕尺寸或方向(自动调整大小的用途取决于视图的定义方式)。

[rootTabBarController.view setAutoResizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

【讨论】:

    【解决方案3】:

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

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

      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 上)才能正确旋转等,我的示例代码包括它(它可以在将来为您节省一些时间)

      【讨论】:

      • 使用这种技术的问题是我有另一个屏幕作为 rootViewController 并且从那个屏幕我来到这个 tabBar 屏幕。如果您有任何其他解决方案,请提出建议!
      猜你喜欢
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 2020-06-02
      • 2017-07-17
      • 1970-01-01
      相关资源
      最近更新 更多