vivek bhoraniya
vivek bhoraniya

Reputation: 1535

Set toolbar color and navigationbar back button

I want to set the background color of the toolbar in iOS7: I am setting the color with this:

toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,40)];
[toolBar setBarStyle:UIBarStyleBlack];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,flexible,barButtonOther,nil];
toolBar.tintColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"dropdownbar"]];

But the background image is not displaying.

enter image description here

And I want to know what this does:

 NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];
    self.navigationController.navigationBar.translucent = NO;
} else {
    self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];

}

Nd how to change back button color of default navigation bar:

enter image description here

enter image description here

Upvotes: 1

Views: 8357

Answers (4)

Jitendra Kulkarni
Jitendra Kulkarni

Reputation: 833

//How to set color of  *all* navigationbars and toolbars in an app in Swift 2.0

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

      func application(application: UIApplication, didFinishLaunchingWithOptionslaunchOptions: [NSObject: AnyObject]?) -> Bool {
         UINavigationBar.appearance().tintColor = UIColor.greenColor(); // All back button in the app are now green.
         UIToolbar.appearance().tintColor = UIColor.orangeColor(); // All toolBar icons in the app are now orange.

        //........
        let returnCode: Bool = getAppSpecificReturnCode();



       return returnCode
    }


    }

Upvotes: 1

Popeye
Popeye

Reputation: 12113

This code is basically determining what the iOS is and selecting the correct method call to set the tintColor of the navigationBar. See comments in code below

/* This is basically getting the systemVersion of the device so 7.0.1 - as this is returned
   as an NSString the user is separating this string based on the string "." which will
   place each string into the array. so you will have 3 objects in this array of "7", "0", "1"
 */
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
/* Once we have separated this string into the array we are getting the first object in that array
   so index `0` which will return a string at which point we are converting the string "7" to
   an intValue and comparing it to the int 7. So if that value is 7 or greater return TRUE
 */
if ([[ver objectAtIndex:0] intValue] >= 7) {
    /* The value was TRUE so lets use the iOS 7 way of setting the tintColor.
       We do this by setting barTintColor for the navigationBar and because
       in iOS 7 the navigationBar can be translucent - by default this is YES
       but because we are setting to a color we want we need to set this to NO
       because we don't want to have it translucent.
     */
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];
    self.navigationController.navigationBar.translucent = NO;
} else {
    /* 
     If all fails in the if statements conditional check we must be on something 
     that is below iOS 7 so use the old way of things and just set the tintColor
     */
    self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];
}

Just so you know this doesn't work. If you are going to use this change it to:

if (floor(NSFoundationVersionNumber) => NSFoundationVersionNumber_iOS_7_0) {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];
    self.navigationController.navigationBar.translucent = NO;
} else {
    self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];
}

this is because if ([[ver objectAtIndex:0] intValue] >= 7) will return FALSE even if iOS 7.

To set the toolbar you could use the same method but replace:

self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];
self.navigationController.navigationBar.translucent = NO;

with

[toolbar setBarTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"dropdownbar"]]];
[toolbar setTranslucent:NO];

and replace

 self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]];

with

 [toolbar setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"dropdownbar"]]];

Upvotes: 2

KIDdAe
KIDdAe

Reputation: 2722

In iOS 7 you have to use toolbar.barTintColor to set a color, toolbar.tintColor will set the color of the barButtons inside the toolbar.

The last piece of code tests if the app is running on iOS 7, if it is barTintColor is used, if it's not, tintColor will be used.

To change the backButton you can do the same by setting tintColor of the navigationBar

Upvotes: 7

Maulik Kundaliya
Maulik Kundaliya

Reputation: 462

you can use following code...

[toolBar setBackgroundImage:toolBarIMG forToolbarPosition:0 barMetrics:0];

Upvotes: 0

Related Questions