簡體   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