我正在另一个 UITabBar
中实现一个 UITabBar
。我的问题是,无论屏幕大小如何,第二个 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;
iPhone6 Plus截图
最佳答案
感谢您提供突出显示按钮的片段。
你想要这样的东西吗?
纵向方向:
我的 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/