[英]Set background color (not Text Color) of Back button in UINavigationBar IOS
Is there any way to set the background color of back button in NavigationBar like this 有没有办法像这样在NavigationBar中设置后退按钮的背景颜色
There are some post available on SO which shows how to set Tint color. SO上有一些帖子,展示了如何设置色调颜色。 But can anyone let me know if there is way to set Background Color like shown in image above.
但是有人可以让我知道是否有如上图所示设置背景颜色的方法。 or i have to set in custom way?
还是我必须以自定义方式设置?
for navigationBar you should setbackgroundImage 对于navigationBar您应该setbackgroundImage
navigationController?.navigationBar.setBackgroundImage(UIImage(named: "your image name"),
forBarMetrics: UIBarMetrics.Default)
I tried with Tint color
, but I can not make it work properly. 我尝试了
Tint color
,但是无法使其正常工作。
I do that by adding an UIView
at the back of the UINavigationBar
. 我通过在
UINavigationBar
的背面添加UIView
来实现。 In that case it must be properly positioned using Autolayout
. 在这种情况下,必须使用
Autolayout
正确Autolayout
。
You have to realize, that is not actually back button background. 您必须意识到,这实际上不是后退按钮的背景。 If you'll take a closer look at navigation bar, you will see back button has a limited width and height.
如果您仔细看一下导航栏,您将看到“后退”按钮的宽度和高度受到限制。 Something like this:
像这样:
So if you really need to customize your navigation bar, you have 2 variants: 因此,如果您确实需要自定义导航栏,则有两种变体:
1) set your navigation bar background image, with fixed or adapting back button background 1)设置导航栏背景图像,固定或调整后退按钮背景
2) implement custom navigation bar, ignoring system UINavigationBar 2)实现自定义导航栏,忽略系统UINavigationBar
You can use custom image and set title manually 您可以使用自定义图像并手动设置标题
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"customImage"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.