简体   繁体   中英

Add custom colors and tintColor to Toolbar using Swift - iOS

I am currently making a project, where I needed to add custom color to toolbar using Swift. I was able to add custom color to navigation bar but I am unable to add customization like navigation bar to toolbar. Is there anything else I am missing? I have researched a lot in StackOverflow about adding the tint color and text color to toolbar but I was not able to do so yet..

My code that customizes the navigation bar is as follows:

    super.viewWillAppear(animated)
    var nav = self.navigationController?.navigationBar
    nav?.barStyle = UIBarStyle.Black
    nav?.tintColor = UIColor.whiteColor()
    nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

For example: If you use email app on iOS devices, you will see toolbar at the bottom of the screen, where Apple have added "Compose Message" icon. I want to customize this toolbar like I am currently doing for Navigation bar above. Any examples will be helpful.

在此处输入图片说明

You need change UIToolBar background image. The image same the color you want to set for your toolbar. See below code.

self.toolBar.setBackgroundImage(UIImage(named: "your-image-name"), forToolbarPosition: .Any, barMetrics: .Default)
//self.toolBar.setShadowImage(UIImage(), forToolbarPosition: .Any)

UPDATED: In Apple doc.

Use these methods to set and access custom background images for toolbars. Default is nil. When non-nil the image will be used instead of the system image for toolbars in the specified position. For the barMetrics argument, UIBarMetricsDefault is the fallback.

DISCUSSION: Interdependence of barStyle, tintColor, backgroundImage. When barStyle or tintColor is set as well as the bar's background image, the bar buttons (unless otherwise customized) will inherit the underlying barStyle or tintColor.

Hope that helps!

Change your nav variable from self.navigationController?.navigationBar to self.navigationItem. That is the one you want to customize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM