2015-10-23 58 views
7

我在另一个UITabBar内执行UITabBar。我的问题是,无论屏幕大小如何,第二个TabBar宽度都保持不变。这在更大的屏幕上突出了很多。我附上截图让你更好地理解。UITabBar宽度不随屏幕大小而增加

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加

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

截图:选择是使用蓝色背景 First TabBar on Top Second on the bottom

Second Tab in Second TabBar Selected

Third tab Selected in Second TabBar Controller

下面的代码表示

+0

这是默认的UITabBar吗?你如何highlite按钮? – user3820674

+0

刚刚添加了该代码 –

+0

如何设置自动布局? – Kreiri

回答

5

谢谢你的高亮按钮片段。
你想要这样的东西吗?
纵向方向:

enter image description here


横向方向:

enter image description here

我的视图控制器的代码:

#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 
+0

不适用于横向。该应用只能用于纵向模式。我想宽度增加与屏幕尺寸 –

+0

所以方法viewDidLayoutSubviews可能不需要。请标记答案是正确的,如果它帮助你。 – user3820674

+0

它没有帮助 –

相关问题