2015-10-23 29 views
7

Tôi đang triển khai UITabBar trong một số khác UITabBar. Vấn đề của tôi là, chiều rộng TabBar thứ hai vẫn không thay đổi bất kể kích thước màn hình. Điều này đứng ra rất nhiều trong màn hình lớn hơn. Tôi đang đính kèm ảnh chụp màn hình để giúp bạn hiểu rõ hơn. Lựa chọn được chỉ định sử dụng một nền màu xanh First TabBar on Top Second on the bottomChiều rộng UITabBar không tăng với kích thước màn hình

Second Tab in Second TabBar Selected

Third tab Selected in Second TabBar Controller

Dưới đây là các mã:

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; 

Các ảnh chụp màn hình từ iPhone6 ​​Thêm

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

+0

Đây có phải là UITabBar mặc định không? Làm thế nào bạn nút highlite? – user3820674

+0

Chỉ cần thêm mã cho số –

+0

Cài đặt tự động trả lời của bạn như thế nào? – Kreiri

Trả lời

5

Cảm ơn bạn đã trích đoạn nội dung của một nút đánh dấu.
Bạn có muốn một cái gì đó như thế này không? định hướng
Portrait:

enter image description here


Cảnh hướng:

enter image description here

Mã của ViewController tôi:

#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

Không dành cho hướng ngang. ứng dụng chỉ có nghĩa là chạy ở chế độ dọc. Tôi muốn chiều rộng tăng với màn hình –

+0

Vì vậy, phương thức viewDidLayoutSubviews có thể không cần. Vui lòng đánh dấu câu trả lời là đúng nếu nó giúp bạn. – user3820674

+0

nó đã không help.sorry –

Các vấn đề liên quan