-->

Why don't my TabBar buttons autoresize on the

2020-07-22 18:11发布

问题:

I'm building a universal iOS app and the iPad version uses a SplitViewController. In the popover view, I have a UITabBarController with two buttons. When it runs on the iPhone, the TabBar buttons correctly stretch to fill the entire width of the view...

...but on the iPad, in the popover view, the buttons don't stretch to fill the entire width...

I'm creating the UITabBarController programmatically...

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

I also tried setting the autoResizeMask inside the InspectionTabBarViewController's loadView method using the following statement...

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

...but that didn't work either. How can I get the UITabBar buttons to fill the entire width of of the view?

Thanks so much in advance for your help!

回答1:

Change UITabBar property itemPositioning to UITabBarItemPositioningFill:

self.tabBar.itemPositioning = UITabBarItemPositioningFill;

Swift version:

tabBar.itemPositioning = .fill

UITabBar itemPositioning reference



回答2:

This can actually be done by setting the setSelectionIndicatorImage on the tab bar. The buttons will resize to your image's width, regardless of iPhone or iPad.

The problem with this is that it's not dynamic, and every time you add or change the number of items in the tab bar, you'll have to create another image.

A solution is to create the image in code and calculate the width by ({tab bar width}/{# of tabbaritems}).

For example:

Where you create the tab bar:

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

The image method:

- (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;
}


回答3:

  1. We can not set size for the UITabbarItem dynamically.
  2. In iPhone it sets (Devices Width/no of Tabbaritems)
  3. In iPad it sets with specific width

If you want to create such kind of view, then go with custom buttons which looks like tab bar buttons and add functionality by your code for the same.



回答4:

I found itemWidth(property of UITabBar) in iOS7.

UITabBar reference