简体   繁体   English

如何将导航栏大标题转换为多行,居中对齐

[英]How to convert Navbar Large title to Multi-line, centre aligned

I'm trying to design view controller with Multi-lined centred Large title text exactly like Ask Siri by apple ( Settings->General->Keyboards->About Ask Siri, Dictation and Privacy... ).我正在尝试设计视图 controller 与多行居中的大标题文本,就像苹果的 Ask Siri ( Settings->General->Keyboards->About Ask Siri, Dictation and Privacy... )。

设置->通用->键盘->关于询问 Siri 滚动时

I can able to achieve centred text using:我可以使用以下方法实现居中文本:

let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
navigationController?.navigationBar.largeTitleTextAttributes = [.paragraphStyle: paragraph]

I did set Navigation title from Storyboard and tried these to achieve multi-lined large title:我确实从 Storyboard 设置了导航标题,并尝试了这些以实现多行大标题:

But none of them are worked on iOS 13.但是它们都没有在 iOS 13 上工作。

You can achieve this by adding a multiline label to your scrollView and then show/hide your navigation item title in the scrollViewDidScroll delegate method of the scrollView depending on the vertical scrollView offset.您可以通过将多行 label 添加到您的 scrollView 来实现此目的,然后根据垂直 scrollView 偏移量在 scrollView 的scrollViewDidScroll委托方法中显示/隐藏您的导航项标题。

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y > myLabelHeight && navigationItem.title == "" {
        setTitle(hidden: false)
    } else if scrollView.contentOffset.y <= myLabelHeight && navigationItem.title == "MyTitleString" {
        setTitle(hidden: true)
    }
}

I've added a layer transition to achieve the fade effect.我添加了一个图层过渡来实现淡入淡出效果。

func setTitle(hidden: Bool) {
    let animation = CATransition()
    animation.duration = 0.25
    animation.type = .fade

    navigationController?.navigationBar.layer.add(animation, forKey: "fadeText")

    if hidden {
        navigationItem.title = ""
    } else {
        navigationItem.title = "MyTitleString"
    }
}

Don't forget to set the navigation item title to an empty string in viewDidLoad .不要忘记在viewDidLoad中将导航项标题设置为空字符串。

There is not any such kind of property that you can set and title became multiline.您无法设置任何此类属性,并且title变为多行。 You need to manipulate it.你需要操纵它。

This is a code example of how you can create a multiline navigationBar title:这是一个如何创建多行导航栏标题的代码示例:

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```

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

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