Adjust UINavigationBar's UIBarButtonItem size and margin in iOS 26
16:30 28 Nov 2025

I want to adjust the size of my navigation bar button item. Here's a minimum repro code:

import UIKit

class ViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    let w: CGFloat = 64
    let h: CGFloat = 64

    let v1 = UIView(frame: CGRectMake(0, 0, w, h))
    let v2 = UIView(frame: CGRectMake(0, 0, w, h))
    let v3 = UIView(frame: CGRectMake(0, 0, w, h))
    let v4 = UIView(frame: CGRectMake(0, 0, w, h))
    let v5 = UIView(frame: CGRectMake(0, 0, w, h))
    let v6 = UIView(frame: CGRectMake(0, 0, w, h))

    v1.backgroundColor = .red
    v2.backgroundColor = .green
    v3.backgroundColor = .yellow
    v4.backgroundColor = .gray
    v5.backgroundColor = .cyan
    v6.backgroundColor = .purple
    
    let views: [UIView] = [v1, v2, v3, v4, v5, v6]
    
    navigationItem.rightBarButtonItems = views.map { v in
      v.anchor(to: CGSizeMake(12, 12))
      let item = UIBarButtonItem(customView: v)
      if #available(iOS 26.0, *) {
        item.hidesSharedBackground = true
      }
      return item
    }
  }
}

extension UIView {
  func anchor(to size: CGSize) {
    translatesAutoresizingMaskIntoConstraints = false
    let constraints = [
      heightAnchor.constraint(equalToConstant: size.height),
      widthAnchor.constraint(equalToConstant: size.width)
    ]
    NSLayoutConstraint.activate(constraints)
  }
}

In my code, each view's size is initialized as 64x64, later I use constraint to force the size to be 12x12.

On iOS 18, the size is correctly updated:

enter image description here

However, on iOS 26, it looks like only the height is respected:

enter image description here

Also, notice the horizontal gap between each item on iOS 26 is larger than iOS 18. How can I preserve my iOS 18 sizes and gaps?

ios uikit