Saif
Saif

Reputation: 2958

UIToolBar with UIBarButtonItem's of different colors

Is there a possibility to have UIToolBar with UIBarButtonItem's of different colors,

For example, The first button should have a red font color, where as the rest of buttons should have lightGray font color.

Note: Here are some considerations :)

  1. I cannot use UITabBar, Though it can be achieved by offsetting title using setTitlePositionAdjustment

  2. I know that title color can be changed using [barButton setTitleTextAttributes:@{NSFontAttributeName:toolBarFont,NSForegroundColorAttributeName:themeColor} forState:UIControlStateNormal];, i cannot change title color of one item.

  3. Cannot use UIBarButtonItem with customView, as the width of items is not automatically adjusted.

Upvotes: 1

Views: 1307

Answers (2)

Ronak Adeshara
Ronak Adeshara

Reputation: 321

You can Add the image as Like Below and add more than one button to UItoolbar

let playimg : UIImage = UIImage(named: "ic_playButton")!
let settingimg : UIImage = UIImage(named: "ic_settingButton")!

let playButton : UIBarButtonItem  = UIBarButtonItem(image: playimg,
            style: UIBarButtonItemStyle.Plain ,
            target: self, action: "playAudio")
playButton.barTintColor = UIColor.redColor()

let settingButton : UIBarButtonItem  = UIBarButtonItem(image: settingimg,
            style: UIBarButtonItemStyle.Plain ,
            target: self, action: "btnSettingAction")
settingButton.barTintColor = UIColor.blueColor()       
self.navigationItem.rightBarButtonItems = [settingButton,playButton]

Swift 4:

let playimg : UIImage = UIImage(named: "ic_playButton")!
        let settingimg : UIImage = UIImage(named: "ic_settingButton")!

        let playButton : UIBarButtonItem  = UIBarButtonItem(image: playimg,
                                                            style: UIBarButtonItemStyle.plain ,
                                                            target: self, action: #selector(self.playAudio))
        playButton.tintColor = UIColor.red

        let settingButton : UIBarButtonItem  = UIBarButtonItem(image: settingimg,
                                                               style: UIBarButtonItemStyle.plain ,
                                                               target: self, action: #selector(self.btnSettingAction))
        settingButton.tintColor = UIColor.blue
        self.navigationItem.rightBarButtonItems = [settingButton,playButton]

Upvotes: 3

user3441734
user3441734

Reputation: 17534

each UIBarButtonItem has tintColor property and you can set it for each UIBarButtonItem object separately ....

import UIKit

let b1 = UIBarButtonItem(title: "Red", style: .Plain, target: nil, action: nil)
b1.tintColor = UIColor.redColor()

let b2 = UIBarButtonItem(title: "Green", style: .Plain, target: nil, action: nil)
b2.tintColor = UIColor.greenColor()

let item = UINavigationItem(title: "nav")
item.leftBarButtonItem = b1
item.rightBarButtonItem = b2

dump(item.titleView?.tintColor)
dump(item.leftBarButtonItem?.tintColor)
dump(item.rightBarButtonItem?.tintColor)

/*
 - nil
 ▿ UIDeviceRGBColorSpace 1 0 0 1
 ▿ Some: UIDeviceRGBColorSpace 1 0 0 1 #0
 ▿ UIDeviceRGBColor: UIDeviceRGBColorSpace 1 0 0 1
 ▿ UIColor: UIDeviceRGBColorSpace 1 0 0 1
 - NSObject: UIDeviceRGBColorSpace 1 0 0 1
 ▿ UIDeviceRGBColorSpace 0 1 0 1
 ▿ Some: UIDeviceRGBColorSpace 0 1 0 1 #0
 ▿ UIDeviceRGBColor: UIDeviceRGBColorSpace 0 1 0 1
 ▿ UIColor: UIDeviceRGBColorSpace 0 1 0 1
 - NSObject: UIDeviceRGBColorSpace 0 1 0 1
*/

Upvotes: 1

Related Questions