2

I have been trying to build a custom UINavigationBar, but my button.frame inputs haven't had any effect.

override func viewDidLoad() {
    super.viewDidLoad()

    let button = UIButton(type: .custom)
    button.setImage(UIImage(named: "I_sent"), for: .normal)
    button.addTarget(self, action: #selector(action), for: .touchUpInside)

    // This doesn't seem to have an effect:
    button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    button.contentMode = .scaleAspectFit

    let item = UIBarButtonItem(customView: button)
    navigationItem.rightBarButtonItem = item
}

@objc func action() {
    print("Action")
}

The code (above) appears like this when run:

But I am trying to make it look like this:

Any help is very much appreciated!

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Jake
  • 23
  • 4

1 Answers1

2

Add constraints to the UIButton as:

button.widthAnchor.constraint(equalToConstant: button.frame.size.width).isActive = true
button.heightAnchor.constraint(equalToConstant: button.frame.size.height).isActive = true

Hope it helps!

Imad Ali
  • 3,261
  • 1
  • 25
  • 33