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.
Change width of a UIBarButtonItem in a UINavigationBar in swift
// Swift 3
let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
backButton.setBackgroundImage(UIImage(named: "img"), for: .normal)
backButton.addTarget(self, action: "action:", for: .touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
// Swift 2
let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
backButton.setBackgroundImage(UIImage(named: "img"), forState: .Normal)
backButton.addTarget(self, action: "action:", forControlEvents: .TouchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
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
Change the width of an UIBarButtonItem
you cant. If you want custom width you will need to use initWithCustomView
Change position of UIBarButtonItem in UINavigationBar
There is no particularly good way to do this. Your best bet if you really must is to subclass UINavigationBar, and override layoutSubviews
to call [super layoutSubviews]
and then find and reposition the button's view.
Resize UIBarButtonItem in code
You can't resize a UIBarButtonItem as you would a UIView. What you can do is change its width property.
UIBarButtonItem *b;
// Initialize and such ...
b.width = 150.0;
This should work for a Fixed Space Bar Button Item.
Related Topics
Querying in Firebase by Child of Child
Fail to Import Restkit with Cocoapods Dynamic Frameworks
iOS Firebase: Firauthuidelegate.Authui Not Being Called
Possible to Write Swift Println Logs into File Too
How to "Explicitly" Implement a Protocol in Swift? If It Is Impossible, Why
Tvos Textfield Transparent Background
Way to Purge All But One Object Types (Models) in a Realm
Opening Import File for Module 'Swift': Not a Directory
Enum Not Working in Custom Initializer
How to Customize the Font and Appearance of a Uialertcontroller in the New Xcode W/ iOS8
How to Make Xcode Put Starting Brace on New Line in Swift
Swift Optional Type: How .None == Nil Works
Passing a Variable Through a Segue? Xcode 8 Swift 3
Capturing a Struct Reference in a Closure Doesn't Allow Mutations to Occur
How to Masking the Last Number in Swift