Is there a way to make a two line prompt for a swift navigation bar? I currently cannot find a property to modify. The text I am currently displaying in the prompt comes from an external data model, so sometimes there is more text than fits on the screen. Thanks.
Asked
Active
Viewed 213 times
1
-
Check this: https://stackoverflow.com/questions/2422383/uinavigationbar-multi-line-title – schinj Jul 12 '17 at 17:17
-
The `prompt` property of `UINavigationItem` only supports one line of text. – rmaddy Jul 12 '17 at 17:25
2 Answers
0
You can try like this:
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

Jaydeep Vora
- 6,085
- 1
- 22
- 40
-
-
@RajanMaheshwari Yes, but width is subject to change, above one is just example which i have used. – Jaydeep Vora Jul 12 '17 at 17:20
-
The question is about the `prompt` property of `UINavigationItem`. This answer isn't really appropriate since there is already a title and also a prompt. – rmaddy Jul 12 '17 at 17:25
-
@rmaddy Is there any way to set the size of text in a prompt then? Thanks. – Matt P Jul 12 '17 at 18:55
-
@MattP I don't think so. There is the `titleTextAttributes` property of `UINavigationBar` but I don't believe that affects the prompt, just the title. – rmaddy Jul 12 '17 at 20:22
0
Navigation bar has a title and a prompt
navigationItem.title = "Title, large"
navigationItem.prompt = "One line prompt, small text, auto-shrink"
Having a prompt could be better that having a custom title view, because it gives you more height and it works great with searchbars. But make sure this is really what you want, since the code bellow is not tested on all devices iOS versions. This will just give you an idea how you can control almost anything regarding layout in the navigation bar
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)
}
}
To use your navigation bar use:
let navVc = UINavigationController(navigationBarClass: MyNavigationBar.self, toolbarClass: nil)

Kamen Dobrev
- 1,321
- 15
- 20