How big should a UIBarButtonItem image be?
As of iOS 11, the Human Interface Guidelines suggest glyphs be about 25×25 points in toolbars and navigation bars, up to a maximum of about 28 points. (And the HIG should definitely be in your bookmarks if you're working on iOS apps!)
That would translate to images 25px square for older devices like iPad 2 / Mini, 50px square for most current devices like iPhone 8 or iPad, and 75px square for Retina HD devices (the iPhone 6/7/8 Plus, or iPhone X). Asset catalogs will help immensely in keeping the different asset sizes organized (and Xcode can even generate them from vector sources these days).
Change size of UIBarButtonItem (image) in Swift 3
In the end I did it like this and it worked:
let moreButton = UIButton(frame: CGRect(x: 0, y: 0, width: 35, height: 35))
moreButton.setBackgroundImage(UIImage(named: "ic_more_vert_3"), for: .normal)
moreButton.addTarget(self, action: #selector(TableViewController.handleMore), for: .touchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: moreButton)
Answer from: Change width of a UIBarButtonItem in a UINavigationBar in swift
iOS 11 UIBarButtonItem images not sizing
BarButtonItem (iOS11\xCode9) uses autolayout instead of frames. Try this (Swift):
if #available(iOS 9.0, *) {
cButton.widthAnchor.constraint(equalToConstant: customViewButton.width).isActive = true
cButton.heightAnchor.constraint(equalToConstant: customViewButton.height).isActive = true
}
Objective C
if (@available(iOS 9, *)) {
[cButton.widthAnchor constraintEqualToConstant: standardButtonSize.width].active = YES;
[cButton.heightAnchor constraintEqualToConstant: standardButtonSize.height].active = YES;
}
iOS: How to set the proper image scale for a bar button item
In images.xcassets, you can add the images as 1x, 2x and 3x. Xcode will use the appropriate image size depending on the device.
Change width of a UIBarButtonItem in a UINavigationBar
One approach you might consider is creating a UIBarButtonItem
by calling initWithCustomView:
. This is not ideal in that you don't get "selected" states out of the box AND you have to composite your bordered background (if want that look) with your button image, but with that you can more directly specify a frame for your toolbar item. If you're using text for your title instead of images you may still need add in a background image as a subview. Anyway, I'm having the same problem right now and this code works for me:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button-image.png"]];
imageView.frame = CGRectMake(0, 0, 43, 30);
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.leftBarButtonItem = barButtonItem;
Right now this is the only way I know of restricting the auto-sizing of the UIBarButtonItem
s added to the UINavigationController
's navigationItem
.
Or try Maggie's solution, which is more thorough than mine.
Related Topics
Uicollectionview Remove Top Padding
How to Put the Image on the Right Side of the Text in a Uibutton
What Is the Life Cycle of an iPhone Application
Uicollectionview Set Number of Columns
Remove Empty Space Before Cells in Uitableview
Updating iOS Badge Without Push Notifications
How to List Out All the Subviews in a Uiviewcontroller in iOS
Customize Mkannotation Callout View
iOS Swiftui: Pop or Dismiss View Programmatically
Rotation Methods Deprecated, Equivalent of 'Didrotatefrominterfaceorientation'
Restoring Animation Where It Left Off When App Resumes from Background
Tweening/Interpolating Between Two Cgpaths/Uibeziers