【问题标题】:UITabBar width not increasing with screen sizeUITabBar 宽度不随屏幕尺寸增加
【发布时间】:2015-10-23 10:14:31
【问题描述】:

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

【问题讨论】:

  • 这是默认的 UITabBar 吗?你如何突出按钮?
  • 刚刚添加了代码
  • 您的自动布局是如何设置的?
  • 自动布局设置“正常”..视图的宽度等于屏幕的宽度............只有第二个选项卡的宽度,停留在“320 “ 更有趣的是,如果我在控制台中打印宽度(self.tabbar.frame.size.width),它显示 375,对于 iphone 6 和 414 对于 iphone 6+ ..但是尺寸在视图中没有改变
  • 尝试调用这个方法(你设置标签栏的宽度)有一些延迟

标签: ios objective-c autolayout uitabbar uitabbaritem


【解决方案1】:

感谢您对突出显示按钮的 sn-p。
你想要这样的东西吗?
纵向:


横向:


我的 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

【讨论】:

  • 不适用于横向。该应用程序仅适用于纵向模式。我希望宽度随着屏幕大小而增加
  • 所以方法 viewDidLayoutSubviews 可能不需要。如果对您有帮助,请将答案标记为正确。
  • 它没有帮助。对不起
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 2021-09-01
  • 2015-05-28
  • 2018-09-21
  • 2016-11-27
相关资源
最近更新 更多