2015-10-23 8 views
7

UITabBarを別のUITabBarに実装しています。私の問題は、画面の大きさにかかわらず、2番目の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ですか?どのようにハイライトボタン? – user3820674

+0

そのコードを追加しました –

+0

自動レイアウトはどのように設定されていますか? – Kreiri

答えて

5

ハイライトボタンのスニペットありがとうございました。
このようなタイプがありますか?
縦向き:

enter image description here


横向き:私のViewControllerの

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

それは助けませんでした。 –

関連する問題