多行导航栏标题

17

我正在尝试设置导航栏中的标题标签以允许多行。我有自定义的导航控制器代码,我正在将多行代码放入其中。我知道已经存在的代码可以工作,但是我的多行部分没有效果。

let titleLabel = UILabel()
titleLabel.frame = CGRectMake(0, 0, self.navigationBar.frame.width, self.navigationBar.frame.height * 2)
titleLabel.numberOfLines = 0
titleLabel.lineBreakMode = .ByWordWrapping

navigationItem.titleView = titleLabel

但是文本仍然溢出了末尾。我也尝试将其放入单独的视图控制器中,添加 self.navigationController?.navigationItem 前面,结果相同。

我的代码有什么问题会导致标题标签使用多行吗?


1
你的 navigationBar.frame.width 可能还没有被调整以适应屏幕。你可能需要重写 viewDidLayoutSubviews 方法来检测正确的框架。 - Cœur
5个回答

46

这里是一个代码示例,展示如何创建多行的导航栏标题

let label: UILabel = UILabel(frame: CGRectMake(0, 0, 400, 50))
label.backgroundColor = UIColor.clearColor()
label.numberOfLines = 2
label.font = UIFont.boldSystemFontOfSize(16.0)
label.textAlignment = .Center
label.textColor = UIColor.whiteColor()
label.text = "This is a\nmultiline string for the navBar"
self.navigationItem.titleView = label

Swift 5.x:

let label = UILabel()
label.backgroundColor = .clear
label.numberOfLines = 2
label.font = UIFont.boldSystemFont(ofSize: 16.0)
label.textAlignment = .center
label.textColor = .white
label.text = "This is a\nmultiline string for the navBar"
self.navigationItem.titleView = label

需要进行一些调整,使其更符合我的要求,但这确实有所帮助。 - Cody Harness
1
我使用了navigationItem.titleView而没有显式设置框架,它仍然可以正常工作。 - gutte
2
@Rashwan L 如果您定义了UILabel的框架,它将导致标签左对齐,因此只需留下框架部分并使用"let label = UILabel()"即可。它将完美地工作并将自己设置为居中对齐。 - Azharhussain Shaikh
为什么宽度是400个点? - Paweł Brewczynski
让标签 = UILabel() 这是居中标题的最佳解决方案! - micdev

4
这在故事板中是可行的。只需将一个UIView拖入导航栏中,然后在文档大纲中将UILabel拖到它上面,将行数设置为2并将对齐方式设置为居中即可。

enter image description here


0

使用此方法可以精确获取标签的位置:

let labelWidth = navBar.bounds.width - 110

    let label = UILabel(frame: CGRect(x:(navBar.bounds.width/2) - (labelWidth/2), y:0, width:labelWidth, height:navBar.bounds.height))
    label.backgroundColor = UIColor.clear
    label.numberOfLines = 0
    label.font = UIFont.boldSystemFont(ofSize: 13.0)
    label.textAlignment = .center
    label.textColor = UIColor.black
    label.lineBreakMode = .byWordWrapping

    label.text = loadedName
    navBar.topItem?.title = nil
    navBar.addSubview(label)

顶部行中的110值是您想要标签两侧的间距。


在SwiftUI中如何实现?有什么提示吗? - David Homes

-1

Swift 5+非常简单的解决方案

func titleMultiLine(topText: String, bottomText: String) {
    //            let titleParameters = [NSForegroundColorAttributeName : UIColor.white,
    //                                   NSFontAttributeName : UIFont.<Font>]
//            let subtitleParameters = [NSForegroundColorAttributeName : UIColor.<Color>(),
    //                                      NSFontAttributeName : UIFont.<Font>]
    let titleParameters = [NSAttributedString.Key.foregroundColor : UIColor.white]
    
    let subtitleParameters = [NSAttributedString.Key.foregroundColor : UIColor.white]
    
    let title:NSMutableAttributedString = NSMutableAttributedString(string: topText, attributes: titleParameters)
    let subtitle:NSAttributedString = NSAttributedString(string: bottomText, attributes: subtitleParameters)
    
    title.append(NSAttributedString(string: "\n"))
    title.append(subtitle)
    
    let size = title.size()
    
    let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
    titleLabel.attributedText = title
    titleLabel.numberOfLines = 0
    titleLabel.textAlignment = .center
    
    navigationItem.titleView = titleLabel
}

函数调用

self.titleMultiLine(topText: "I am top text Title", bottomText: "bottom text")

-1
将以下代码添加到您的 ViewController 中:
navigationItem.setValue(1, forKey: "__largeTitleTwoLineMode")

私有API可能不是最好的选择方式 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接