UITabBar宽度不随屏幕尺寸增加而增加

7
我正在实现一个在另一个UITabBar中的UITabBar。我的问题是,无论屏幕大小如何,第二个TabBar的宽度都保持不变。这在大屏幕上非常突出。我附上一张截图以让您更好地理解。蓝色背景表示选定状态。 First TabBar on Top Second on the bottom Second Tab in Second TabBar Selected Third tab Selected in Second TabBar Controller 以下是代码:
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;
来自iPhone6 Plus的截屏

第一个选项卡栏的第二个标签被选中(不是问题的一部分,只是为了展示整张图片)


这是默认的UITabBar吗?你如何突出显示按钮? - user3820674
刚刚加入了这个代码。 - Sidharth J Dev
你的自动布局设置如何? - Kreiri
你是如何在另一个 UITabBar 中实现 UITabBar 的?据我所知,UITabBar 的高度始终为 49,并且只能包含 UITabBarItem 类的对象。 - Reinhard Männer
@ReinhardMänner 我有两个TabBarControllers-主控制器和第二个Tab Bar控制器。 Tab Main Controller的第一个项目连接到导航控制器,该导航控制器连接到下一个TabBarController(Second Tab Bar Controller)。它们的高度都是49,但我设置了框架,使得Second Tab Bar Controller在Tab Main Controller下面。 - Sidharth J Dev
显示剩余3条评论
1个回答

5
谢谢您提供的亮点按钮片段。 您是想要这样的效果吗? 纵向方向:

enter image description here

横向布局: enter image description here 我的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

不支持横屏模式。该应用程序仅支持竖屏模式运行。我希望宽度随着屏幕尺寸增加而增加。 - Sidharth J Dev
所以viewDidLayoutSubviews方法可能不需要。如果它对您有帮助,请将答案标记为正确。 - user3820674
没有帮助,抱歉。 - Sidharth J Dev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接