2012-03-09 83 views
2

我正在構建一個通用的iOS應用程序,並且iPad版本使用了SplitViewController。在popover視圖中,我有一個帶有兩個按鈕的UITabBarController。當它運行在iPhone上使用TabBar按鈕正確地拉伸以填充視圖的整個寬度...爲什麼我的TabBar按鈕不能在iPad上自動調整大小?

enter image description here

...但在iPad上,在酥料餅看來,按鈕不拉伸以填充整個寬度...

enter image description here

我創建的UITabBarController編程...

InspectionTabBarViewController *inspectionTabBarVC; 
    InspectionListViewController *inspectionListVC; 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 

     inspectionListVC = [[InspectionListViewController alloc] initWithSunday:NO]; 
     inspectionListVC.managedObjectContext = self.managedObjectContext; 
     UINavigationController *calendarNavVC = [[UINavigationController alloc] initWithRootViewController:inspectionListVC]; 
     calendarNavVC.title = @"Calendar"; 

     InspectionMapViewController *mapViewVC = [[InspectionMapViewController alloc] initWithNibName:@"InspectionMapView_iPhone" bundle:nil]; 
     UINavigationController *mapdNavVC = [[UINavigationController alloc] initWithRootViewController:mapViewVC]; 
     mapdNavVC.title = @"Map"; 

     inspectionTabBarVC = [[InspectionTabBarViewController alloc] init]; 
     [inspectionTabBarVC addChildViewController:calendarNavVC]; 
     [inspectionTabBarVC addChildViewController:mapdNavVC]; 
     self.window.rootViewController = inspectionTabBarVC; 
    } 
    else 
    { 
     inspectionListVC = [[InspectionListViewController alloc] initWithSunday:NO]; 
     UINavigationController *calendarNavVC = [[UINavigationController alloc] initWithRootViewController:inspectionListVC]; 
     calendarNavVC.title = @"Calendar"; 

     InspectionMapViewController *mapViewVC = [[InspectionMapViewController alloc] initWithNibName:@"InspectionMapView_iPad" bundle:nil]; 
     UINavigationController *mapdNavVC = [[UINavigationController alloc] initWithRootViewController:mapViewVC]; 
     mapdNavVC.title = @"Map"; 

     inspectionTabBarVC = [[InspectionTabBarViewController alloc] init]; 
     [inspectionTabBarVC addChildViewController:calendarNavVC]; 
     [inspectionTabBarVC addChildViewController:mapdNavVC]; 

     DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPad" bundle:nil]; 
     UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController]; 

     self.splitViewController = [[UISplitViewController alloc] init]; 
     self.splitViewController.delegate = detailViewController; 
     self.splitViewController.viewControllers = [NSArray arrayWithObjects:inspectionTabBarVC, detailNavigationController, nil]; 

     self.window.rootViewController = self.splitViewController; 
     inspectionListVC.detailViewController = detailViewController; 
     inspectionListVC.managedObjectContext = self.managedObjectContext; 

     detailViewController.detailViewControllerDelegate = inspectionListVC; 
    } 

    [self.window makeKeyAndVisible]; 

我也嘗試使用下面的語句設置autoResizeMask的InspectionTabBarViewController的方法的loadView內...

self.tabBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth; 

...但是,這也不能工作。我如何獲得UITabBar按鈕來填充視圖的整個寬度?

非常感謝您的幫助!

+0

1.我們不能爲UITabbarItem集大小動態。 – Mrunal 2012-03-09 05:13:20

+0

如果有幫助,請將其標記爲已接受的答案。 – Mrunal 2014-12-30 10:03:15

回答

1
  1. 我們無法動態設置UITabbarItem的大小。
  2. 在iPhone它設置(設備寬度/無Tabbaritems的)
  3. 在iPad上也設置與特定寬度

如果你想創造這樣一種觀點,然後用它看起來像標籤自定義按鈕去條形按鈕,並通過您的代碼添加相同的功能。

4

這實際上可以通過在標籤欄上設置setSelectionIndicatorImage來完成。無論iPhone或iPad如何,按鈕都會根據圖像寬度調整大小。

這裏的問題是,它不是動態的,每次添加或更改標籤欄項目的數量時,你就必須創建另一個形象。

一種解決方案是在代碼中創建圖像,並計算由({tab bar width}/{# of tabbaritems})的寬度。

例如:

當您創建標籤欄:

[[self.tabBarController tabBar] setSelectionIndicatorImage:[self selectionIndicatorImage]]; 

圖像方法:

- (UIImage *)selectionIndicatorImage 
{ 
    NSUInteger count = [[self.tabBarController viewControllers] count]; 
    CGSize tabBarSize = [[self.tabBarController tabBar] frame].size; 
    NSUInteger padding = 2; 

    CGSize buttonSize = CGSizeMake(tabBarSize.width/count, tabBarSize.height); 

    UIGraphicsBeginImageContext(buttonSize); 

    CGContextRef c = UIGraphicsGetCurrentContext(); 

    [[UIColor colorWithWhite:0.9 alpha:0.1] setFill]; 

    UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(padding, padding * 2, buttonSize.width - (padding * 2) , buttonSize.height - (padding * 2)) cornerRadius:4.0]; 

    [roundedRect fillWithBlendMode: kCGBlendModeNormal alpha:1.0f]; 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    CGContextRelease(c); 

    return image; 
} 
+0

使用resizableImageWithCapInsets怎麼樣,這可以創建一個自動延伸的圖像,我認爲。 – Aardvark 2013-04-03 19:38:14

+0

僅供參考,這仍適用於iOS 8。 – mcphersonjr 2015-04-13 16:28:15

11

變化UITabBar財產itemPositioningUITabBarItemPositioningFill

self.tabBar.itemPositioning = UITabBarItemPositioningFill; 

斯威夫特版本:

tabBar.itemPositioning = .fill 

UITabBar itemPositioning reference

相關問題