2015-10-23 19 views
7

Ich bin ein UITabBar innerhalb einer anderen UITabBar implementieren. Mein Problem ist, dass die zweite TabBar-Breite unabhängig von der Bildschirmgröße konstant bleibt. Dies fällt in den größeren Bildschirmen auf. Ich füge einen Screenshot an, damit Sie es besser verstehen. Die Auswahl wird angezeigt einen blauen Hintergrund mit First TabBar on Top Second on the bottomUITabBar Breite nicht mit Bildschirmgröße

Second Tab in Second TabBar Selected

Third tab Selected in Second TabBar Controller

Hier ist der Code:

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; 

die Screenshots von iPhone6 ​​Plus-

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

+0

Ist diese Standard-UITabBar? Wie sind deine Highlite-Tasten? – user3820674

+0

Nur den Code dafür hinzugefügt –

+0

Wie ist Ihr Auto-Layout eingerichtet? – Kreiri

Antwort

5

Vielen Dank für Snippet eines Highlight-Buttons.
Wollte Sie so etwas?
Portrait Orientierung:

enter image description here


Landschaft Orientierung:

enter image description here

-Code meiner 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 
+0

Nicht für Querformat. Die App soll nur im Hochformat laufen. Ich möchte die Breite mit der Bildschirmgröße –

+0

So Methode viewDidLayoutSubviews wahrscheinlich nicht brauchen. Bitte markieren Sie die Antwort als richtig, wenn sie Ihnen geholfen hat. – user3820674

+0

es hat nicht geholfen.sorry –

Verwandte Themen