UIBarButtonItem TintColor 与 UINavigationController
我的导航栏中有一个带有 2 个 UIBarButtonItems 的导航控制器。我只想更改右侧颜色的色调。我找到了一种静态方法来做到这一点:
[[self.navigationController.navigationBar.subviews objectAtIndex:2] setTintColor:[UIColor redColor]];
问题是当我将控制器推入导航控制器以显示另一个视图时,当我回到根视图时,我的右侧导航栏按钮应该具有自定义颜色,按钮的颜色恢复为默认颜色。当我再次点击它时,应用程序崩溃了。它说它无法更改色调颜色,就像导航栏中该元素的索引发生变化一样。
我尝试过在互联网上找到的其他技术,但是当我使用导航控制器并返回到根控制器时,它们都失败了......
有什么想法吗? 谢谢!
编辑 1:
我想要在 UINavigationBar 中添加一个带有红色或绿色背景颜色的边框样式按钮。
关于我发现的其他方法,它几乎是导航栏中视图的 foreach 循环,如果视图的类是按钮项,则更改tintColor。它不会崩溃,但它适用于我的导航栏的所有 UIBarButtonItem(我只想要一个特定的按钮,正确的按钮,而不是全部)。例如本教程是一半工作,返回根视图控制器时,我的应用程序崩溃了。
I have a navigation controller with 2 UIBarButtonItems in my navigation bar. I want to change the tint color only for the one on the right. I have found a way in static to do that:
[[self.navigationController.navigationBar.subviews objectAtIndex:2] setTintColor:[UIColor redColor]];
The problem is when I push a controller into my navigation controller to display another view, when I come back to the root view where my right navigation bar button is supposed to have a custom color, the color of the button is back to its default. And when I click again on it, the app crashes. It says it cannot change the tint color, like if the index for this element in my navigation bar changed.
I have tried other technics found on the internet, but they all failed when I use the navigation controller and come back to the root controller...
Any idea?
Thanks!
Edit 1:
I would like a bordered style button in my UINavigationBar, with a red or green background color.
Regarding the other ways I found, it is pretty much a foreach loop of the views in the navigation bar, and if the view's kind of class is a button item then change the tintColor. It doesn't crash but it applies to all the UIBarButtonItem of my navigation bar (and I just want a specific button, the right one, not all of them). For example this tutorial is half working, my app crashes when coming back to the root view controller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
深入研究 navigationcontroller.navigation 栏的子视图不会与 Apple 一起飞行,...
更改 UIBarButtonItem 颜色的正确方法是将 customView 与按钮项一起使用。这是一个解释的链接...
UIBarButtonItem 带有颜色?
Digging into the subviews of the navigationcontroller.navigation bar wont fly with Apple, ...
the correct way to change the color of a UIBarButtonItem is to use a customView with the buttonitem. here is a link that explains...
UIBarButtonItem with color?
您只需创建一个只有一个段的分段控件即可。根据您的喜好设置其色调颜色。您可能还想将其模式设置为瞬时,这样它在视觉上就像一个按钮。使用 initWithCustomView: 初始化器将分段控件添加到栏按钮项。这就是您通常创建自定义有色按钮的方式。
例子:
You simply create a segmented control with just one segment. Set its tint color as you like. You may also want to set its mode to momentary so it optically behaves like a button. Add the segmented control to the bar button item by using the initWithCustomView: initializer. That's how you typically create custom tinted buttons.
Example: