skip to Main Content

Requirement

Required Design

I am only able to bring the title at centre

how it can be done?

2

Answers


  1. Custom view, set navigationItem.titleView.

    Login or Signup to reply.
  2. You can create a custom view and set it as the titleView of the navigationItem.

    TitleView.xib

    • Add a UIView and create the layout as you wish.
    • Set the backgroundColor of the View and ViewController to clear.

    enter image description here

    TitleView.swift

    import UIKit
    
    class TitleView: UIView {
    
        @IBOutlet weak var imageView: UIImageView!
        @IBOutlet weak var label1: UILabel!
        @IBOutlet weak var label2: UILabel!
        
        class func instanceFromNib() -> TitleView {
            return UINib(nibName: "TitleView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! TitleView
    
        }
        
        override var intrinsicContentSize: CGSize {
            return UIView.layoutFittingExpandedSize
        }
    }
    

    ViewController.swift

     override func viewDidLoad() {
          super.viewDidLoad()
            
          self.navigationItem.titleView = TitleView.instanceFromNib()
     }
    

    Preview

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search