Okan
Okan

Reputation: 1389

Putting imageview on navigation bar

I want to add image to right side of navigation bar. But I can't drag image view to navigation bar. I can only drag a button.

enter image description here

How can I do this ? I want same thing with whatsapp profile image. You know they have circle profile picture on right side of navigation bar.

Upvotes: 4

Views: 8175

Answers (3)

Sai kumar Reddy
Sai kumar Reddy

Reputation: 1829

**This will Display the image on right side on navigation bar**

enter image description here

 func viewDidLoad(){
        let containView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
                let imageview = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
                imageview.image = UIImage(named: "photo.jpg")
                imageview.contentMode = UIViewContentMode.scaleAspectFit
                imageview.layer.cornerRadius = 20
                imageview.layer.masksToBounds = true
                containView.addSubview(imageview)
                let rightBarButton = UIBarButtonItem(customView: containView)
                self.navigationItem.rightBarButtonItem = rightBarButton
        }

Upvotes: 9

Rory McKinnel
Rory McKinnel

Reputation: 8014

Drag a UIView and drop it on the navigation bar first. Then drag a UIImageView into the view.

That should give you what you are looking for.

Set the image view height and width using constraints. You have to set the width and height of the containing view in the size inspector.

Upvotes: 9

Vasyl Khmil
Vasyl Khmil

Reputation: 2557

Don't know about storyboards. But from code you can create UIBarButtonItem with custom view.

        let customView = UIImageView()
        let customViewItem = UIBarButtonItem(customView: customView)
        self.navigationItem.rightBarButtonItem = customViewItem

From storyboard:

Just drag UIBarButtonItem into your controller navigation bar. In element menu(Attribute inspector) select identifier: Custom and Image: the image you want.

Upvotes: 2

Related Questions