alanwong alanwong - 1 month ago 20
iOS Question

Change the toolbar color

It is working for navigationBar:

var colour = UIColor.red
self.navigationController?.navigationBar.tintColor = colour


But do not work for toolbar:

self.navigationController?.toolbar.tintColor = colour


I searched the internet and stack overflow. No answer is workable for me.
Some people said:

self.toolbar.barTintColor = UIColor.redColor()


It is also not working for me. (value of type 'thisView' has no member 'toolbar')

I want to edit the toolbar color in coding. No change in the storyboard setting. Thanks.

EDIT:
I am working on adding a toolbar under the webview. Like go back, stop, reload.
https://i.stack.imgur.com/lzvQo.png

//

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var webView: UIWebView!

var colour = UIColor.red

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = colour

// problem in here ..........................
self.navigationController?.toolbar.tintColor = UIColor.black


let URL = NSURL(string: "https://www.apple.com")
webView.loadRequest(NSURLRequest(url: URL! as URL) as URLRequest)
}



@IBAction func backButton(_ sender: AnyObject) {
webView.goBack()
}


@IBAction func nextButton(_ sender: AnyObject) {
webView.goForward()
}

@IBAction func refreshButton(_ sender: AnyObject) {
webView.reload()
}

@IBAction func stopbutton(_ sender: AnyObject) {
webView.stopLoading()
}


}

Answer

If you've just got a toolbar that is on a ViewController in Your Storyboard all you need to do is add an IBOutlet to your View Controller and connect the toolbar in the storyboard to that outlet. This code goes in your ViewController

@IBOutlet var toolbar: UIToolbar?

Then, in the storyboard, hold the control button and click drag from View Controller (in the left sidebar) to your toolbar. This will create a connection between the toolbar in the storyboard to the toolbar var in your code. After that connection is made all you need to do is set the barTintColor on that toolbar variable like so:

self.toolbar.barTintColor = UIColor.blue
Comments