简体   繁体   English

使用Swift将自定义颜色和tintColor添加到工具栏-iOS

[英]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. 我目前正在制作一个项目,需要在其中使用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.. 我已经在StackOverflow中进行了很多研究,目的是将色调颜色和文本颜色添加到工具栏,但是我还不能这样做。

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. 例如:如果您在iOS设备上使用电子邮件应用程序,您将在屏幕底部看到工具栏,苹果在其中添加了“撰写邮件”图标。 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. 您需要更改UIToolBar背景图像。 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. 对于barMetrics参数,UIBarMetricsDefault是后备。

DISCUSSION: Interdependence of barStyle, tintColor, backgroundImage. 讨论: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. 设置barStyle或tintColor以及条的背景图像时,条按钮(除非另行定制)将继承基础的barStyle或tintColor。

Hope that helps! 希望有帮助!

Change your nav variable from self.navigationController?.navigationBar to self.navigationItem. 将您的nav变量从self.navigationController?.navigationBar更改为self.navigationItem。 That is the one you want to customize. 那就是您要自定义的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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