简体   繁体   English

通过在 Swift 中添加两个标签而不是标题来自定义导航栏

[英]Customize navigation bar by adding two labels instead of title in Swift

I am trying to add two labels in the place where the title is shown in navigation bar, but I am struggling to do so.我试图在导航栏中显示标题的位置添加两个标签,但我正在努力这样做。 It would be very nice if I could achieve this with storyboard but as I can see I cannot do it.如果我能用故事板实现这一点,那就太好了,但正如我所见,我做不到。

As I have seen I need to use navigationItem but I do not know how exactly to do that.正如我所见,我需要使用 navigationItem,但我不知道具体如何做到这一点。 If anyone have any example or if anyone could explain me more specifically how to do so would be wonderful.如果有人有任何例子,或者有人可以更具体地向我解释如何做到这一点,那就太好了。

And I need to mention that I am completely unfamiliar with Obj-C, so any help would need to be in Swift.我需要提一下,我完全不熟悉 Obj-C,所以任何帮助都需要在 Swift 中进行。

I am not sure if you can do it from the storyboard, but if you want to add two title labels, you can do the following in the viewDidLoad() method of the view controller for which you want the two titles:我不确定您是否可以从故事板中执行此操作,但是如果您想添加两个标题标签,您可以在您想要两个标题的视图控制器的viewDidLoad()方法中执行以下操作:

if let navigationBar = self.navigationController?.navigationBar {
    let firstFrame = CGRect(x: 0, y: 0, width: navigationBar.frame.width/2, height: navigationBar.frame.height)
    let secondFrame = CGRect(x: navigationBar.frame.width/2, y: 0, width: navigationBar.frame.width/2, height: navigationBar.frame.height)

    let firstLabel = UILabel(frame: firstFrame)
    firstLabel.text = "First"

    let secondLabel = UILabel(frame: secondFrame)
    secondLabel.text = "Second"

    navigationBar.addSubview(firstLabel)
    navigationBar.addSubview(secondLabel)
}

In this way you can add as many subviews as you want to the navigation bar通过这种方式,您可以向导航栏添加任意数量的子视图

Here's an implementation that uses a stack view instead, which also gives you some versatility with layout of the labels:这是一个使用堆栈视图的实现,它还为您提供了标签布局的多功能性:

class ViewController: UIViewController {

    lazy var titleStackView: UIStackView = {
        let titleLabel = UILabel()
        titleLabel.textAlignment = .center
        titleLabel.text = "Title"
        let subtitleLabel = UILabel()
        subtitleLabel.textAlignment = .center
        subtitleLabel.text = "Subtitle"
        let stackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
        stackView.axis = .vertical
        return stackView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.titleView = titleStackView
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        if view.traitCollection.horizontalSizeClass == .compact {
            titleStackView.axis = .vertical
            titleStackView.spacing = UIStackView.spacingUseDefault
        } else {
            titleStackView.axis = .horizontal
            titleStackView.spacing = 20.0
        }
    }
}

演示使用堆栈视图创建自定义导航项的标题视图

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

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