双线提示 - 斯威夫特

时间:2017-07-12 17:06:02

标签: ios swift uinavigationbar

有没有办法为快速导航栏提供两行提示?我目前找不到要修改的属性。我当前在提示中显示的文本来自外部数据模型,因此有时会有更多文本不适合屏幕。感谢。

Image Showing Text Outside of Frame

2 个答案:

答案 0 :(得分:0)

您可以尝试这样:

Swift 3.0

let label = UILabel(frame: CGRect(x:0, y:0, width:350, height:50)) //width is subject to change, Defined as per your screen
label.backgroundColor =.clear
label.numberOfLines = 2
label.font = UIFont.boldSystemFont(ofSize: 16.0)
label.textAlignment = .center
label.textColor = UIColor.white
label.text = "Your Text here"
self.navigationItem.titleView = label

答案 1 :(得分:0)

导航栏具有标题和提示

navigationItem.title = "Title, large"
navigationItem.prompt = "One line prompt, small text, auto-shrink"  

有一个提示可能比拥有一个自定义标题视图更好,因为它可以为您提供更多的高度,并且可以与搜索栏一起使用。但是请确保这确实是您想要的,因为未在所有iOS版本的设备上都测试过以下代码。这只会让您了解如何控制导航栏中几乎所有与布局有关的内容

class MyNavigationBar: UINavigationBar {
func allSubViews(views: [UIView]) {
    for view in views {
        if let label = view as? UILabel, label.adjustsFontSizeToFitWidth {
            if label.numberOfLines != 2 { //this is the promp label
                label.numberOfLines = 2
                let parent = label.superview
                parent?.frame = CGRect(x: 0, y: 0, width: parent!.bounds.width, height: 44)

                parent!.removeConstraints(parent!.constraints)
                label.removeConstraints(label.constraints)
                label.leadingAnchor.constraint(equalTo: parent!.leadingAnchor, constant: 20).isActive = true
                label.trailingAnchor.constraint(equalTo: parent!.trailingAnchor, constant: -20).isActive = true
                label.topAnchor.constraint(equalTo: parent!.topAnchor).isActive = true
                label.bottomAnchor.constraint(equalTo: parent!.bottomAnchor).isActive = true
            }
            return
        }
        
        self.allSubViews(views: view.subviews)
    }
}

override func layoutSubviews() {
    super.layoutSubviews()
    allSubViews(views: self.subviews)
}
}

要使用导航栏,请使用:

let navVc = UINavigationController(navigationBarClass: MyNavigationBar.self, toolbarClass: nil)
相关问题