pochimen
pochimen

Reputation: 307

Add UIImage below UINavigationBar bottom Swift

I'd like to add an UIImage below my navigationbar.

enter image description here

So far, my code:

let imageView = UIImageView(frame: CGRect(x: 0, y: 150, width: 38, height: 38))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "logo.png")
imageView.image = image
navigationItem.titleView = imageView

The logo is still in the middle of the NavigationBar, not below

Upvotes: 0

Views: 118

Answers (1)

ifau
ifau

Reputation: 2120

As documentation says, titleView will be displayed in the center of navigation bar.

UINavigationBar ignores frame of view which was assigned to titleView and layout it to center. You can create a view-container, assign it to titleView, and place imageView inside it:

let imageView = UIImageView(frame: CGRect(x: 0, y: 150, width: 38, height: 38))
imageView.contentMode = .ScaleAspectFit
imageView.image = UIImage(named: "logo.png")

let containerView = UIView()
containerView.clipsToBounds = false
containerView.addSubview(imageView)
navigationItem.titleView = containerView

Upvotes: 1

Related Questions