为什么我的TabBar按钮不会在iPad上自动显示?

时间:2012-03-09 04:59:03

标签: iphone ios ipad uitabbarcontroller

我正在构建一个通用iOS应用程序,iPad版本使用SplitViewController。在popover视图中,我有一个带有两个按钮的UITabBarController。当它在iPhone上运行时,TabBar按钮会正确拉伸以填充视图的整个宽度......

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];

我还尝试使用以下语句在InspectionTabBarViewController的loadView方法中设置autoResizeMask ...

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

......但这也不起作用。如何让UITabBar按钮填充整个视图宽度?

非常感谢您的帮助!

4 个答案:

答案 0 :(得分:11)

UITabBar属性itemPositioning更改为UITabBarItemPositioningFill

self.tabBar.itemPositioning = UITabBarItemPositioningFill;

Swift版本:

tabBar.itemPositioning = .fill

UITabBar itemPositioning reference

答案 1 :(得分: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;
}

答案 2 :(得分:1)

  1. 我们无法动态设置UITabbarItem的大小。
  2. 在iPhone中设置(设备宽度/没有Tabbaritems)
  3. 在iPad中设置特定宽度
  4. 如果你想创建这种类型的视图,那么使用看起来像标签栏按钮的自定义按钮,并按照你的代码添加功能。

答案 3 :(得分:0)

我在iOS7中找到了itemWidth(UITabBar的属性)。

UITabBar reference