Zmiana odcienia / koloru tła paska UITabBar

UINavigationBar i uisearchbar mają właściwość tintColor, która pozwala na zmianę koloru tint (zaskakujące, wiem) obu tych elementów. Chcę zrobić to samo do UITabBar w mojej aplikacji, ale znalazłem teraz sposób, aby zmienić go z domyślnego koloru czarnego. Jakieś pomysły?

Author: pixel, 2009-02-20

18 answers

Udało mi się sprawić, by działało poprzez podklasowanie kontrolera UITabBarController i użycie klas prywatnych:

@interface UITabBarController (private)
- (UITabBar *)tabBar;
@end

@implementation CustomUITabBarController


- (void)viewDidLoad {
    [super viewDidLoad];

    CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
    UIView *v = [[UIView alloc] initWithFrame:frame];
    [v setBackgroundColor:kMainColor];
    [v setAlpha:0.5];
    [[self tabBar] addSubview:v];
    [v release];

}
@end
 48
Author: coneybeare,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-14 17:17:56

System IOS 5 dodał kilka nowych metod dostosowywania wyglądu większości elementów interfejsu użytkownika.

Możesz kierować każdą instancję paska UITabBar w aplikacji za pomocą proxy wygląd.

Dla iOS 5 + 6:

[[UITabBar appearance] setTintColor:[UIColor redColor]];
W przypadku systemu iOS 7 i nowszych należy użyć następujących elementów:]}
[[UITabBar appearance] setBarTintColor:[UIColor redColor]];

Używanie proxy wyglądu zmieni każdą instancję paska kart w aplikacji. Dla konkretnej instancji użyj jednej z nowych właściwości tej klasy:

UIColor *tintColor; // iOS 5+6
UIColor *barTintColor; // iOS 7+
UIColor *selectedImageTintColor;
UIImage *backgroundImage;
UIImage *selectionIndicatorImage;
 104
Author: imnk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-17 11:03:40

Mam dodatek do ostatecznej odpowiedzi. Chociaż podstawowy schemat jest poprawny, sztuczka z użyciem częściowo przezroczystego koloru może zostać ulepszona. Zakładam, że chodzi tylko o to, aby domyślny gradient wyświetlał się przez. No i wysokość paska Tabb wynosi 49 pikseli, a nie 48, przynajmniej w OS 3.

Tak więc, jeśli masz odpowiedni obraz 1 x 49 z gradientem, to jest to wersja viewDidLoad, której powinieneś użyć:

- (void)viewDidLoad {

    [super viewDidLoad]; 

    CGRect frame = CGRectMake(0, 0, 480, 49);
    UIView *v = [[UIView alloc] initWithFrame:frame];
    UIImage *i = [UIImage imageNamed:@"GO-21-TabBarColorx49.png"];
    UIColor *c = [[UIColor alloc] initWithPatternImage:i];
    v.backgroundColor = c;
    [c release];
    [[self tabBar] addSubview:v];
    [v release];

}
 34
Author: pabugeater,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-31 07:50:22

Kiedy tylko użyjesz addSubview Twoje przyciski stracą klikalność, więc zamiast

[[self tabBar] addSubview:v];

Użycie:

[[self tabBar] insertSubview:v atIndex:0];
 27
Author: Marcin Zbijowski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-21 17:49:20

Following jest idealnym rozwiązaniem dla tego. To działa dobrze ze mną Dla iOS5 i iOS4.

//---- For providing background image to tabbar
UITabBar *tabBar = [tabBarController tabBar]; 

if ([tabBar respondsToSelector:@selector(setBackgroundImage:)]) {
    // ios 5 code here
    [tabBar setBackgroundImage:[UIImage imageNamed:@"image.png"]];
} 
else {
    // ios 4 code here
    CGRect frame = CGRectMake(0, 0, 480, 49);
    UIView *tabbg_view = [[UIView alloc] initWithFrame:frame];
    UIImage *tabbag_image = [UIImage imageNamed:@"image.png"];
    UIColor *tabbg_color = [[UIColor alloc] initWithPatternImage:tabbag_image];
    tabbg_view.backgroundColor = tabbg_color;
    [tabBar insertSubview:tabbg_view atIndex:0];
}
 7
Author: Gaurav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-15 17:57:47

Na iOS 7:

[[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:(38.0/255.0) green:(38.0/255.0) blue:(38.0/255.0) alpha:1.0]];

Polecam również ustawienie najpierw w zależności od twoich wizualnych pragnień:

[[UITabBar appearance] setBarStyle:UIBarStyleBlack];

Styl paska umieszcza subtelny separator między zawartością widoku a paskiem kart.

 7
Author: capikaw,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-02 18:46:44

Nie ma prostego sposobu, aby to zrobić, w zasadzie musisz podklasować UITabBar i zaimplementować Niestandardowy rysunek, aby zrobić to, co chcesz. To sporo pracy na efekt, ale może warto. Polecam zgłoszenie błędu z Apple, aby dodać go do przyszłego iPhone SDK.

 6
Author: Louis Gerbarg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-02-20 20:11:21

[[self tabBar] insertSubview:v atIndex:0]; mi pasuje idealnie.

 5
Author: jimmy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-04 19:15:52

Dla mnie to bardzo proste, aby zmienić kolor Tabbara jak: -

[self.TabBarController.tabBar setTintColor:[UIColor colorWithRed:0.1294 green:0.5686 blue:0.8353 alpha:1.0]];


[self.TabBarController.tabBar setTintColor:[UIColor "YOUR COLOR"];
Spróbuj tego!!!
 5
Author: Muhammad Rizwan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-01 09:34:44
 [[UITabBar appearance] setTintColor:[UIColor redColor]];
 [[UITabBar appearance] setBarTintColor:[UIColor yellowColor]];
 3
Author: Hsm,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-31 01:16:24

For just background color

Tabbarcontroller.tabBar.barTintColor=[UIColor redcolour];

Lub to w App Delegate

[[UITabBar appearance] setBackgroundColor:[UIColor blackColor]];

Do zmiany koloru ikon odznacz tabbara

Dla iOS 10:

// this code need to be placed on home page of tabbar    
for(UITabBarItem *item in self.tabBarController.tabBar.items) {
    item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

Nad iOS 10:

// this need to be in appdelegate didFinishLaunchingWithOptions
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blackColor]];
 2
Author: Vaibhav Gaikwad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-05 10:24:30

Istnieje kilka dobrych pomysłów w istniejących odpowiedziach, wiele z nich działa nieco inaczej, a to, co wybierzesz, będzie również zależeć od tego, które urządzenia celujesz i jaki wygląd dążysz do osiągnięcia. UITabBar jest notorycznie nieintuicyjny, jeśli chodzi o dostosowywanie wyglądu, ale oto kilka dodatkowych sztuczek, które mogą pomóc: {]}

1). Jeśli chcesz pozbyć się błyszczącej nakładki, aby uzyskać bardziej płaski wygląd, wykonaj następujące czynności:]}

tabBar.backgroundColor = [UIColor darkGrayColor]; // this will be your background
[tabBar.subviews[0] removeFromSuperview]; // this gets rid of gloss

2). Aby ustawić niestandardowe obrazy na przyciskach paska kart, zrób coś like:

for (UITabBarItem *item in tabBar.items){
    [item setFinishedSelectedImage:selected withFinishedUnselectedImage:unselected];
    [item setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)];
}

Gdzie selected i unselectedUIImage obiektami do wyboru. Jeśli chcesz, aby były płaskie, najprostszym rozwiązaniem, jakie znalazłem, jest stworzenie UIView z pożądanym backgroundColor, a następnie renderowanie go w UIImage za pomocą QuartzCore. Używam poniższej metody w kategorii na UIView, aby uzyskać UIImage z zawartością widoku:

- (UIImage *)getImage {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [[UIScreen mainScreen]scale]);
    [[self layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

3) na koniec możesz dostosować styl tytułów przycisków. Do:

for (UITabBarItem *item in tabBar.items){
    [item setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                [UIColor redColor], UITextAttributeTextColor,
                [UIColor whiteColor], UITextAttributeTextShadowColor,
                [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
                [UIFont boldSystemFontOfSize:18], UITextAttributeFont,
            nil] forState:UIControlStateNormal];
}

To pozwala zrobić kilka korekty, ale wciąż dość ograniczone. W szczególności nie można dowolnie modyfikować miejsca, w którym tekst jest umieszczony w przycisku, i nie można mieć różnych kolorów dla zaznaczonych / niezaznaczonych przycisków. Jeśli chcesz zrobić bardziej szczegółowy układ tekstu, Ustaw UITextAttributeTextColor, aby był przejrzysty i dodaj swój tekst do obrazów selected i unselected z części (2).

 1
Author: SaltyNuts,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-28 19:17:55
[v setBackgroundColor ColorwithRed: Green: Blue: ];
 1
Author: Salim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-16 12:13:32

Innym rozwiązaniem (które jest hack) jest ustawienie Alfy na tabBarController na 0.01 tak, że jest praktycznie niewidoczny, ale nadal klikalny. Następnie Ustaw kontrolkę an ImageView na dole stalówki okna głównego z niestandardowym obrazem paska Tabb pod alpha ' ed tabBarCOntroller. Następnie zamień obrazy, Zmień kolory lub podświetlenie, gdy tabbarcontroller przełącza widoki.

Jednak tracisz '...więcej ' i dostosować funkcjonalność.

 0
Author: user855723,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-03 11:09:13

Cześć, używam iOS SDK 4 i udało mi się rozwiązać ten problem za pomocą tylko dwóch linii kodu i to idzie tak

tBar.backgroundColor = [UIColor clearColor];
tBar.backgroundImage = [UIImage imageNamed:@"your-png-image.png"];
Mam nadzieję, że to pomoże!
 0
Author: Jorge Vicente Mendoza,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-22 17:55:18
if ([tabBar respondsToSelector:@selector(setBackgroundImage:)]) {
    // ios 5 code here
    [tabBar setBackgroundImage:[UIImage imageNamed:@"image.png"]];
} 
else {
    // ios 4 code here
    CGRect frame = CGRectMake(0, 0, 480, 49);
    UIView *tabbg_view = [[UIView alloc] initWithFrame:frame];
    UIImage *tabbag_image = [UIImage imageNamed:@"image.png"];
    UIColor *tabbg_color = [[UIColor alloc] initWithPatternImage:tabbag_image];
    tabbg_view.backgroundColor = tabbg_color;
    [tabBar insertSubview:tabbg_view atIndex:0];
}
 0
Author: user1049755,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-07 16:30:02

Swift 3.0 odpowiedź: (z Vaibhav Gaikwad)

Do zmiany koloru odznaczonych ikon tabbara:

if #available(iOS 10.0, *) {
        UITabBar.appearance().unselectedItemTintColor = UIColor.white
    } else {
        // Fallback on earlier versions
        for item in self.tabBar.items! {
            item.image = item.image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        }
}

Tylko do zmiany koloru tekstu:

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .normal)

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red, for: .selected)
 0
Author: odemolliens,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-14 15:17:43

Swift 3 używając wyglądu z twojego AppDelegate wykonaj następujące czynności:

UITabBar.appearance().barTintColor = your_color

 0
Author: Bobj-C,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-04 18:49:26