ios - UITabBar 宽度不随屏幕尺寸增加

标签 ios objective-c autolayout uitabbar uitabbaritem

我正在另一个 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截图

Second Tab Of First TabBar selected(not part of the question, just to show the full picture)

最佳答案

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

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

关于ios - UITabBar 宽度不随屏幕尺寸增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33299935/

相关文章:

ios - 使用 UISearchResults 过滤时出现问题

iphone - 打字时如何将文本字段格式设置为十进制?

ios - 你如何平衡自动布局和创建框架?

ios - NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9801)

ios - 横向 iPhone 6 Plus 的 UIModalPresentationPopover 不显示弹出窗口

ios - 将 canDisplayBannerAds 更改为 NO 不会更新 View 以删除广告横幅

iOS - 内联显示多个标签

ios - 从 NSTimer 回到主线程

objective-c - 在 Objective C 中使用带有重载方法的 Swift 类

ios - UIViewController 的 subview 最初在什么时候明确布局?