0

I am trying to display a multi-line titleView in my app. Here is my code to set the titleView.

self.titleView = TitleView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: self.tableView.frame.width, height: 80)), title: "NEWS", subtitle: Date.dateAsStringForNavBarSubTitle())
        self.titleView.backgroundColor = UIColor.red

        self.navigationItem.titleView = self.titleView

The part of the implementation of TitleView is shown below:

 private func setupUI() {

        let label = UILabel(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: self.frame.width, height: self.frame.height)))
        label.numberOfLines = 2
        label.text = "\(self.title)\n\(self.subtitle)"
        self.addSubview(label)
    }

The label comes out to be outside the parent view.

enter image description here

Should I just use a UILabel instead of putting it in UIView?

john doe
  • 9,220
  • 23
  • 91
  • 167
  • Possible duplicate of [UINavigationBar multi-line title](https://stackoverflow.com/questions/2422383/uinavigationbar-multi-line-title) – moghya Jan 03 '19 at 17:49

1 Answers1

4

I can see that you are adding label subview into title view. Instead, you can try this in your viewcontroller's viewDidLoad() method.

For Swift 4

let label = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: 44.0))
label.backgroundColor = .red
label.numberOfLines = 2
label.textAlignment = .left
label.text = "\(self.title)\n\(self.subtitle)"
self.navigationItem.titleView = label 

For more information please refer this link UINavigationBar multi-line title