UITabBar宽度不随屏幕尺寸增加

Sid*_*Dev 7 objective-c uitabbaritem uitabbar ios autolayout

UITabBar在另一个内部实施UITabBar.我的问题是,无论屏幕大小如何,第二个TabBar宽度保持不变.这在更大的屏幕中脱颖而出.我附上了一个截图,让你更了解.使用蓝色背景指示选择 第一个TabBar在顶部第二个在底部

选中第二个TabBar中的第二个选项卡

第三个选项卡在第二个TabBar控制器中选择

这是代码:

GRect rect = CGRectMake(0, 0, self.tabBar.frame.size.width/2, self.tabBar.frame.size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,
                                   [[UIColor colorWithRed:102.0/255.0 green:197.0/255.0 blue:234.0/255.0 alpha:1.0] CGColor]);

    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.tabBar.selectionIndicatorImage = img;
Run Code Online (Sandbox Code Playgroud)

来自iPhone6 Plus的截图

选择第一个TabBar的第二个选项卡(不是问题的一部分,只是为了显示完整的图片)

use*_*674 5

感谢您提供突出显示按钮的片段.
你想要这样的东西吗?
纵向:

在此输入图像描述


横向:

在此输入图像描述

我的ViewController的代码:

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITabBar *tabBar;
@property (weak, nonatomic) IBOutlet UITabBar *topTabBar;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UITabBar appearance] setTintColor:[UIColor redColor]];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName :  [UIFont boldSystemFontOfSize:20]} forState:UIControlStateNormal];
}

- (void)viewDidLayoutSubviews {

    [self highlightTabBarItems:self.tabBar];
    [self highlightTabBarItems:self.topTabBar];
}

- (void)highlightTabBarItems:(UITabBar*)currentTabBar {

    CGFloat highlightedWidth = self.view.frame.size.width/currentTabBar.items.count;
    [currentTabBar setItemWidth:highlightedWidth];
    CGRect rect = CGRectMake(0, 0, highlightedWidth, currentTabBar.frame.size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:102.0/255.0 green:197.0/255.0 blue:234.0/255.0 alpha:1.0] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    currentTabBar.selectionIndicatorImage = img;
}

@end
Run Code Online (Sandbox Code Playgroud)