2015-10-23 21 views
7

Sto implementando uno UITabBar all'interno di un altro UITabBar. Il mio problema è che la seconda larghezza TabBar rimane costante indipendentemente dalle dimensioni dello schermo. Questo spicca molto negli schermi più grandi. Allego uno screenshot per farti capire meglio. La selezione è indicata con uno sfondo blu First TabBar on Top Second on the bottomUITabBar larghezza non crescente con dimensioni dello schermo

Second Tab in Second TabBar Selected

Third tab Selected in Second TabBar Controller

Ecco il codice:

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; 

Le schermate da iPhone6 ​​Inoltre

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

+0

È questo UITabBar predefinito? Come premi i pulsanti? – user3820674

+0

Appena aggiunto il codice per quello –

+0

Come viene impostato il vostro autolayout? – Kreiri

risposta

5

Grazie per il frammento di un pulsante di evidenziazione.
Volevi qualcosa del genere? orientamento
Ritratto:

enter image description here


orientamento orizzontale:

enter image description here

Codice della mia 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

Non per l'orientamento orizzontale. l'app è pensata solo per funzionare in modalità verticale. Voglio aumentare la larghezza con la schermata –

+0

Quindi il metodo viewDidLayoutSubviews probabilmente non è necessario. Per favore contrassegna la risposta come corretta se ti ha aiutato. – user3820674

+0

non ha aiutato –

Problemi correlati