UINavigationBar多行标题
是否有一种直接的方法可以覆盖导航控制器内导航栏中当前导航栏项目的 titleView?我尝试创建一个新的 UIView 并用我自己的 UIVIew 替换 topView 的 titleView 属性,但没有成功。
基本上,我想要一个多行标题作为导航栏标题。有什么建议吗?
Is there a straightforward way of overriding the titleView of the current navigation bar item in a navigation bar within a navigation controller? I've tried creating a new UIView and replacing the titleView property of topView with my own UIVIew with no success.
Basically, I want a multi-line title for the navigation bar title. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
设置
UINavigationItem
的titleView
属性。例如,在视图控制器的viewDidLoad
方法中,您可以执行以下操作:它显示如下:
记住
titleView
属性是 忽略如果leftBarButtonItem
不是nil
。Set the
titleView
property of theUINavigationItem
. For example, in the view controller'sviewDidLoad
method you could do something like:It shows up like this:
Remember the
titleView
property is ignored ifleftBarButtonItem
is notnil
.对于斯威夫特:
对于斯威夫特4:
for Swift:
for swift 4:
Swift 解决方案:
NavigationBar
中的 2 行:BarButton
结果中的 2 行 -
Swift solution:
2 lines in
NavigationBar
:2 line in
BarButton
result -
经过大量调整后,我仍然无法让 Petert 的解决方案在 iOS 8 中为我工作。这是适用于 iOS 8/9 的可复制粘贴的解决方案。感谢 Matt Curtis 的 github 帖子
After a lot of tweaking, I still couldn't get petert's solution to work for me in iOS 8. Here's a copy-paste-able solution for iOS 8/9. Credit goes to Matt Curtis's github post
当标签不居中时该怎么办
如果您遇到与我相同的问题 - 由于后退按钮,标签未在 navigationItem 中居中,请将您的 UILabel 嵌入到 UIView 中。然后,UILabel 不会被迫随其文本一起增长,但当其宽度提高视图宽度时停止增长。有关此问题的更多信息,您可以在这里找到:无法将 titleView 设置在导航栏的中心,因为后退按钮(达伦的回答)
未居中:
居中:
What to do when label is not centered
If you encounter same issue as me - that label is not centered in navigationItem because of back button, embed your UILabel to UIView. UILabel is then not forced to grow with it's text, but stop growing when it's width raise view's width. More about this issue you can find here: Can't set titleView in the center of navigation bar because back button ( Darren's answer )
Not centered:
Centered:
这是处理多行标题的 Swift 3 版本:
Here is a Swift 3 version of handling a multiline title:
这是Swift 4的实现方法 -
Here's a Swift 4 way of doing it-
Swift 4
字体、颜色和导航栏高度均在此处进行硬编码。
Swift 4
Font, color, and navigation bar height are hardcoded here.
在 Swift 5 中,
In Swift 5,
大多数解决方案(除了 @gbk 中的解决方案)都对 UIView(包装视图)和 UILabel 使用硬编码高度 44pt。一切都是由代码创建的。我忽略了动态读取导航栏高度的@gbk 解决方案。
我在 iOS 11 (iPhone 5s) 上使用
orientation = Landscape
时遇到了问题。标签的高度不会调整,当我为横向设置一行文本时,文本与导航栏的底部对齐。不知何故,我发现我可以在 Storyboard 中添加 UILabel 并为此创建一个 IBOutlet。那不是更好吗?
Navigation Item
下。IBOutlet
,您就可以像往常一样使用它。要具有不同大小的文本,请使用属性字符串(上面有很多解决方案)。我在装有 iOS 11.2.6 的 iPhone 5s 上进行了测试,文本正好位于中心位置,没有任何问题,在纵向和横向上都可以正常工作。
Most of the solutions, except the one from @gbk, use hardcoded height 44pt for the UIView (wrapper view) and UILabel. All are created by codes. I overlooked @gbk solution which dynamically read height of navigation bar.
I ran into problem when
orientation = landscape
on iOS 11 (iPhone 5s). The label's height won't adjust and when I set one line of text for landscape, the text align to bottom of navigation bar.Somehow I found that I can add the UILabel in Storyboard and create an IBOutlet for that. Isn't that nicer?
Navigation Item
on the scene panel (on left).IBOutlet
on your view controller and you can use it as usual. To have different size of text, use attribute string (lots of solutions above).I tested on iPhone 5s with iOS 11.2.6 and the text just position in center with no problem, work fine on portrait and landscape.
Swift 5+ https://stackoverflow.com/a/68739808/6881070
一个功能链接中非常简单流畅的解决方案是提到
Swift 5+ https://stackoverflow.com/a/68739808/6881070
very easy and smooth solution in one function link is mention