Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sonnguyen0310/6720cbf39ce877c20fea1a987543fb99 to your computer and use it in GitHub Desktop.
Save sonnguyen0310/6720cbf39ce877c20fea1a987543fb99 to your computer and use it in GitHub Desktop.
iOS 11: Change size of UIBarButtonItem (image) in Swift 3
func setUpMenuButton(){
let menuBtn = UIButton(type: .custom)
menuBtn.frame = CGRect(x: 0.0, y: 0.0, width: 20, height: 20)
menuBtn.setImage(UIImage(named:"menuIcon"), for: .normal)
menuBtn.addTarget(self, action: #selector(vc.onMenuButtonPressed(_:)), for: UIControlEvents.touchUpInside)
let menuBarItem = UIBarButtonItem(customView: menuBtn)
let currWidth = menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 24)
currWidth?.isActive = true
let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 24)
currHeight?.isActive = true
self.navigationItem.leftBarButtonItem = menuBarItem
}
@lpbas
Copy link

lpbas commented Jan 13, 2019

Thank you so much! My UIBarButtonItem with a custom settings icon was too wide for some unexplained reason (the PDF icon was a square) and this was the only way to get it to the correct size.

@heybetzadec
Copy link

Thanks man!

@krmao
Copy link

krmao commented Sep 11, 2019

great

@updev0612
Copy link

Really thank you.
I lost whole night for this.
But your code save me from risk.
Thank you again.

@PredragJ
Copy link

Thanks man. I lost to much time for this! Cheers!

@pradeepktg
Copy link

Thank you so much! I was going mad resolving this issue. Cheers!!!

@Kanokchai-ama
Copy link

Thank you so much man. 🤝

@Scankiran
Copy link

Thanks man. You save me a lot work.

@SreekanthPingThis
Copy link

Great man

@ucwealth
Copy link

Woah! You're a Godsend. Thanks a lot for this!

@JaewoongLee-swift
Copy link

You save my time. Thank you so much :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment