iosswiftxcodejsqmessagesviewcontroller

Centre image in navigation bar


I'm calling a JSQMessageViewController and adding an image as the title but it's not centred due to the offset caused by the Back left-button.

enter image description here

Here's my code for adding the image:

let imageView = UIImageView()
    imageView.frame.size.width = 40
    imageView.frame.size.height = 40
    imageView.contentMode = .scaleAspectFit
    let image = UIImage(named: "avatar_example")
    imageView.image = image
    navigationItem.titleView = imageView

Thanks :)


Solution

  • Are you sure that the problem is in JSQMessageViewController? Maybe you just need to use standard sizes from title view (44*44) for alignment.

        let imageView = UIImageView(image: UIImage(named: "avatar_example"))
        imageView.contentMode = .scaleAspectFit
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
        imageView.frame = titleView.bounds
        titleView.addSubview(imageView)
    
        self.navigationItem.titleView = titleView