Change width of a UIBarButtonItem in a UINavigationBar
I am creating a UIBarButtonItem and adding it to my navigation bar like so:
(void)viewDidLoad {
...
// Add the refresh button to the navigation bar
UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
[refreshButton setFrame:CGRectMake(0,0,30,30)];
[refreshButton setImage:[UIImage imageNamed:@"G_refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *refreshBarButton = [[[UIBarButtonItem alloc] initWithCustomView:refreshButton] autorelease];
self.navigationItem.leftBarButtonItem = refreshBarButton;
}
It looks correct when I run, but I can select the bar button item by tapping the navigation bar anywhere from x = 0 to roughly 100. How can I adjust the selectable area to have a width of 30 px?
ios uinavigationbar uibarbuttonitem uinavigationitem
add a comment |
I am creating a UIBarButtonItem and adding it to my navigation bar like so:
(void)viewDidLoad {
...
// Add the refresh button to the navigation bar
UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
[refreshButton setFrame:CGRectMake(0,0,30,30)];
[refreshButton setImage:[UIImage imageNamed:@"G_refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *refreshBarButton = [[[UIBarButtonItem alloc] initWithCustomView:refreshButton] autorelease];
self.navigationItem.leftBarButtonItem = refreshBarButton;
}
It looks correct when I run, but I can select the bar button item by tapping the navigation bar anywhere from x = 0 to roughly 100. How can I adjust the selectable area to have a width of 30 px?
ios uinavigationbar uibarbuttonitem uinavigationitem
2
I'm afraid I don't think there is a way to adjust the selectable area. A question though, why would you want the width to be 30 px? iOS Human Interface Guideline states that the comfortable minimum size of tappable UI elements is 44 x 44 points. developer.apple.com/library/ios/#documentation/UserExperience/…
– Abd Sani Abd Jalal
Jun 12 '12 at 0:36
Hmm, ok thanks for the link to the guidelines. It seems a bit strange that the selectable area is so wide though.
– Darren
Jun 12 '12 at 1:23
@AbdSaniAbdJalal can we change the width ofbarbuttonitem
in storyboard?
– aircraft
Nov 1 '16 at 3:22
add a comment |
I am creating a UIBarButtonItem and adding it to my navigation bar like so:
(void)viewDidLoad {
...
// Add the refresh button to the navigation bar
UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
[refreshButton setFrame:CGRectMake(0,0,30,30)];
[refreshButton setImage:[UIImage imageNamed:@"G_refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *refreshBarButton = [[[UIBarButtonItem alloc] initWithCustomView:refreshButton] autorelease];
self.navigationItem.leftBarButtonItem = refreshBarButton;
}
It looks correct when I run, but I can select the bar button item by tapping the navigation bar anywhere from x = 0 to roughly 100. How can I adjust the selectable area to have a width of 30 px?
ios uinavigationbar uibarbuttonitem uinavigationitem
I am creating a UIBarButtonItem and adding it to my navigation bar like so:
(void)viewDidLoad {
...
// Add the refresh button to the navigation bar
UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
[refreshButton setFrame:CGRectMake(0,0,30,30)];
[refreshButton setImage:[UIImage imageNamed:@"G_refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *refreshBarButton = [[[UIBarButtonItem alloc] initWithCustomView:refreshButton] autorelease];
self.navigationItem.leftBarButtonItem = refreshBarButton;
}
It looks correct when I run, but I can select the bar button item by tapping the navigation bar anywhere from x = 0 to roughly 100. How can I adjust the selectable area to have a width of 30 px?
ios uinavigationbar uibarbuttonitem uinavigationitem
ios uinavigationbar uibarbuttonitem uinavigationitem
asked Jun 11 '12 at 23:51
DarrenDarren
5,9091654101
5,9091654101
2
I'm afraid I don't think there is a way to adjust the selectable area. A question though, why would you want the width to be 30 px? iOS Human Interface Guideline states that the comfortable minimum size of tappable UI elements is 44 x 44 points. developer.apple.com/library/ios/#documentation/UserExperience/…
– Abd Sani Abd Jalal
Jun 12 '12 at 0:36
Hmm, ok thanks for the link to the guidelines. It seems a bit strange that the selectable area is so wide though.
– Darren
Jun 12 '12 at 1:23
@AbdSaniAbdJalal can we change the width ofbarbuttonitem
in storyboard?
– aircraft
Nov 1 '16 at 3:22
add a comment |
2
I'm afraid I don't think there is a way to adjust the selectable area. A question though, why would you want the width to be 30 px? iOS Human Interface Guideline states that the comfortable minimum size of tappable UI elements is 44 x 44 points. developer.apple.com/library/ios/#documentation/UserExperience/…
– Abd Sani Abd Jalal
Jun 12 '12 at 0:36
Hmm, ok thanks for the link to the guidelines. It seems a bit strange that the selectable area is so wide though.
– Darren
Jun 12 '12 at 1:23
@AbdSaniAbdJalal can we change the width ofbarbuttonitem
in storyboard?
– aircraft
Nov 1 '16 at 3:22
2
2
I'm afraid I don't think there is a way to adjust the selectable area. A question though, why would you want the width to be 30 px? iOS Human Interface Guideline states that the comfortable minimum size of tappable UI elements is 44 x 44 points. developer.apple.com/library/ios/#documentation/UserExperience/…
– Abd Sani Abd Jalal
Jun 12 '12 at 0:36
I'm afraid I don't think there is a way to adjust the selectable area. A question though, why would you want the width to be 30 px? iOS Human Interface Guideline states that the comfortable minimum size of tappable UI elements is 44 x 44 points. developer.apple.com/library/ios/#documentation/UserExperience/…
– Abd Sani Abd Jalal
Jun 12 '12 at 0:36
Hmm, ok thanks for the link to the guidelines. It seems a bit strange that the selectable area is so wide though.
– Darren
Jun 12 '12 at 1:23
Hmm, ok thanks for the link to the guidelines. It seems a bit strange that the selectable area is so wide though.
– Darren
Jun 12 '12 at 1:23
@AbdSaniAbdJalal can we change the width of
barbuttonitem
in storyboard?– aircraft
Nov 1 '16 at 3:22
@AbdSaniAbdJalal can we change the width of
barbuttonitem
in storyboard?– aircraft
Nov 1 '16 at 3:22
add a comment |
6 Answers
6
active
oldest
votes
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.
2
only worked when I usedUIButton
as customView, and notUIImageView
– Kreutzer
Jun 10 '14 at 7:30
1
this solution won't work as it does not have button actions at all. UseUIButton
instead.
– Raptor
Nov 4 '15 at 9:49
add a comment |
Since iOS 11, just setting frame for customView
and not UIBarButtonItem
won't work (like suggested in accepted answer). It seems like adding Autolayout to UIBarButtonItem
overrides the set frame.
This post gave me the idea:
navigationItem.leftBarButtonItem?.customView = yourCustomButtonView
let desiredWidth = 35.0
let desiredHeight = 35.0
let widthConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredWidth)
let heightConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredHeight)
yourCustomButtonView.addConstraints([widthConstraint, heightConstraint])
Also note, that it is preferred that you use UIButton
as your CustomView
as you have to rely on it to trigger the action.
Awesome! And thanks for the link
– Fraser
Aug 2 '18 at 0:07
It was quite surprising to find out that the nav bar uses Auto Layout by default to place items in it, very useful info here.
– noobular
Oct 12 '18 at 23:32
Thanks! Its important to know that manipulatinng the frame won't do nothing anymore
– Hendrik
Oct 22 '18 at 12:08
add a comment |
You can do it by dropping an image to the bar button item from the interface builder and changing the width of the custom view with this code:
CGRect frame = self.navigationItem.leftBarButtonItem.customView.frame;
frame.size.width = 141;
self.navigationItem.leftBarButtonItem.customView.frame = frame;
add a comment |
The key thing with this is to realise that you are changing the custom view's width, rather than the UIBarButton
itself.
The code is therefore:
CGRect resizedFrame = myBarButtonItem.customView.frame;
resizedFrame.size.width = myNewWidth;
myBarButtonItem.customView.frame = resizedFrame;
You will also need to trigger the layout change:
[myNavigationBar setNeedsLayout];
All this goes without saying that the layout is being done with Auto Sizing and frames. Incursions into navigation bars with Auto Layout yielded no success. See my question Auto Layout with UINavigationBar and UIBarButtonItem.
Sorry just realised that my code is almost identical to @oscartzombie. Not intentional! I'll leave this answer as I think it's worth adding the layout and other points, in addition to explaining without reference to Interface Bulder or images specifically.
I was having issues with the size of my custom UIBarButtonItem, the solution was calling[self.navigationController.navigationBar setNeedsLayout];
in my view controller's-(void)viewDidLoad
method.
– LunaCodeGirl
Nov 13 '14 at 20:06
add a comment |
The following approach worked, see code to change width and height of the button and to add action item as well.
UIButton *imageButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
[imageButton setBackgroundImage:[UIImage imageNamed:@"message_button"] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(messageButtonTapped) forControlEvents:UIControlEventAllEvents];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageButton];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
Note: The target and action of the UIBarButtonItem does not apply to image view
add a comment |
None of those solutions worked for me and I figure out auto-resizing thing overrides dimensions that you wrote in code.
after creating button by UIButton way, write following code block:
[self.navigationItem.rightBarButtonItem.customView.widthAnchor constraintEqualToConstant:mWidth].active = YES;
[self.navigationItem.rightBarButtonItem.customView.heightAnchor constraintEqualToConstant:mHeight].active = YES;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10988918%2fchange-width-of-a-uibarbuttonitem-in-a-uinavigationbar%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
2
only worked when I usedUIButton
as customView, and notUIImageView
– Kreutzer
Jun 10 '14 at 7:30
1
this solution won't work as it does not have button actions at all. UseUIButton
instead.
– Raptor
Nov 4 '15 at 9:49
add a comment |
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.
2
only worked when I usedUIButton
as customView, and notUIImageView
– Kreutzer
Jun 10 '14 at 7:30
1
this solution won't work as it does not have button actions at all. UseUIButton
instead.
– Raptor
Nov 4 '15 at 9:49
add a comment |
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.
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.
edited Apr 4 '18 at 22:17
Josh Caswell
58.6k12127176
58.6k12127176
answered Dec 3 '12 at 22:36
AaronAaron
5,70722647
5,70722647
2
only worked when I usedUIButton
as customView, and notUIImageView
– Kreutzer
Jun 10 '14 at 7:30
1
this solution won't work as it does not have button actions at all. UseUIButton
instead.
– Raptor
Nov 4 '15 at 9:49
add a comment |
2
only worked when I usedUIButton
as customView, and notUIImageView
– Kreutzer
Jun 10 '14 at 7:30
1
this solution won't work as it does not have button actions at all. UseUIButton
instead.
– Raptor
Nov 4 '15 at 9:49
2
2
only worked when I used
UIButton
as customView, and not UIImageView
– Kreutzer
Jun 10 '14 at 7:30
only worked when I used
UIButton
as customView, and not UIImageView
– Kreutzer
Jun 10 '14 at 7:30
1
1
this solution won't work as it does not have button actions at all. Use
UIButton
instead.– Raptor
Nov 4 '15 at 9:49
this solution won't work as it does not have button actions at all. Use
UIButton
instead.– Raptor
Nov 4 '15 at 9:49
add a comment |
Since iOS 11, just setting frame for customView
and not UIBarButtonItem
won't work (like suggested in accepted answer). It seems like adding Autolayout to UIBarButtonItem
overrides the set frame.
This post gave me the idea:
navigationItem.leftBarButtonItem?.customView = yourCustomButtonView
let desiredWidth = 35.0
let desiredHeight = 35.0
let widthConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredWidth)
let heightConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredHeight)
yourCustomButtonView.addConstraints([widthConstraint, heightConstraint])
Also note, that it is preferred that you use UIButton
as your CustomView
as you have to rely on it to trigger the action.
Awesome! And thanks for the link
– Fraser
Aug 2 '18 at 0:07
It was quite surprising to find out that the nav bar uses Auto Layout by default to place items in it, very useful info here.
– noobular
Oct 12 '18 at 23:32
Thanks! Its important to know that manipulatinng the frame won't do nothing anymore
– Hendrik
Oct 22 '18 at 12:08
add a comment |
Since iOS 11, just setting frame for customView
and not UIBarButtonItem
won't work (like suggested in accepted answer). It seems like adding Autolayout to UIBarButtonItem
overrides the set frame.
This post gave me the idea:
navigationItem.leftBarButtonItem?.customView = yourCustomButtonView
let desiredWidth = 35.0
let desiredHeight = 35.0
let widthConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredWidth)
let heightConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredHeight)
yourCustomButtonView.addConstraints([widthConstraint, heightConstraint])
Also note, that it is preferred that you use UIButton
as your CustomView
as you have to rely on it to trigger the action.
Awesome! And thanks for the link
– Fraser
Aug 2 '18 at 0:07
It was quite surprising to find out that the nav bar uses Auto Layout by default to place items in it, very useful info here.
– noobular
Oct 12 '18 at 23:32
Thanks! Its important to know that manipulatinng the frame won't do nothing anymore
– Hendrik
Oct 22 '18 at 12:08
add a comment |
Since iOS 11, just setting frame for customView
and not UIBarButtonItem
won't work (like suggested in accepted answer). It seems like adding Autolayout to UIBarButtonItem
overrides the set frame.
This post gave me the idea:
navigationItem.leftBarButtonItem?.customView = yourCustomButtonView
let desiredWidth = 35.0
let desiredHeight = 35.0
let widthConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredWidth)
let heightConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredHeight)
yourCustomButtonView.addConstraints([widthConstraint, heightConstraint])
Also note, that it is preferred that you use UIButton
as your CustomView
as you have to rely on it to trigger the action.
Since iOS 11, just setting frame for customView
and not UIBarButtonItem
won't work (like suggested in accepted answer). It seems like adding Autolayout to UIBarButtonItem
overrides the set frame.
This post gave me the idea:
navigationItem.leftBarButtonItem?.customView = yourCustomButtonView
let desiredWidth = 35.0
let desiredHeight = 35.0
let widthConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredWidth)
let heightConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredHeight)
yourCustomButtonView.addConstraints([widthConstraint, heightConstraint])
Also note, that it is preferred that you use UIButton
as your CustomView
as you have to rely on it to trigger the action.
answered Apr 5 '18 at 15:13
roxanneMroxanneM
31836
31836
Awesome! And thanks for the link
– Fraser
Aug 2 '18 at 0:07
It was quite surprising to find out that the nav bar uses Auto Layout by default to place items in it, very useful info here.
– noobular
Oct 12 '18 at 23:32
Thanks! Its important to know that manipulatinng the frame won't do nothing anymore
– Hendrik
Oct 22 '18 at 12:08
add a comment |
Awesome! And thanks for the link
– Fraser
Aug 2 '18 at 0:07
It was quite surprising to find out that the nav bar uses Auto Layout by default to place items in it, very useful info here.
– noobular
Oct 12 '18 at 23:32
Thanks! Its important to know that manipulatinng the frame won't do nothing anymore
– Hendrik
Oct 22 '18 at 12:08
Awesome! And thanks for the link
– Fraser
Aug 2 '18 at 0:07
Awesome! And thanks for the link
– Fraser
Aug 2 '18 at 0:07
It was quite surprising to find out that the nav bar uses Auto Layout by default to place items in it, very useful info here.
– noobular
Oct 12 '18 at 23:32
It was quite surprising to find out that the nav bar uses Auto Layout by default to place items in it, very useful info here.
– noobular
Oct 12 '18 at 23:32
Thanks! Its important to know that manipulatinng the frame won't do nothing anymore
– Hendrik
Oct 22 '18 at 12:08
Thanks! Its important to know that manipulatinng the frame won't do nothing anymore
– Hendrik
Oct 22 '18 at 12:08
add a comment |
You can do it by dropping an image to the bar button item from the interface builder and changing the width of the custom view with this code:
CGRect frame = self.navigationItem.leftBarButtonItem.customView.frame;
frame.size.width = 141;
self.navigationItem.leftBarButtonItem.customView.frame = frame;
add a comment |
You can do it by dropping an image to the bar button item from the interface builder and changing the width of the custom view with this code:
CGRect frame = self.navigationItem.leftBarButtonItem.customView.frame;
frame.size.width = 141;
self.navigationItem.leftBarButtonItem.customView.frame = frame;
add a comment |
You can do it by dropping an image to the bar button item from the interface builder and changing the width of the custom view with this code:
CGRect frame = self.navigationItem.leftBarButtonItem.customView.frame;
frame.size.width = 141;
self.navigationItem.leftBarButtonItem.customView.frame = frame;
You can do it by dropping an image to the bar button item from the interface builder and changing the width of the custom view with this code:
CGRect frame = self.navigationItem.leftBarButtonItem.customView.frame;
frame.size.width = 141;
self.navigationItem.leftBarButtonItem.customView.frame = frame;
answered Jan 11 '13 at 15:27
odmodm
6091015
6091015
add a comment |
add a comment |
The key thing with this is to realise that you are changing the custom view's width, rather than the UIBarButton
itself.
The code is therefore:
CGRect resizedFrame = myBarButtonItem.customView.frame;
resizedFrame.size.width = myNewWidth;
myBarButtonItem.customView.frame = resizedFrame;
You will also need to trigger the layout change:
[myNavigationBar setNeedsLayout];
All this goes without saying that the layout is being done with Auto Sizing and frames. Incursions into navigation bars with Auto Layout yielded no success. See my question Auto Layout with UINavigationBar and UIBarButtonItem.
Sorry just realised that my code is almost identical to @oscartzombie. Not intentional! I'll leave this answer as I think it's worth adding the layout and other points, in addition to explaining without reference to Interface Bulder or images specifically.
I was having issues with the size of my custom UIBarButtonItem, the solution was calling[self.navigationController.navigationBar setNeedsLayout];
in my view controller's-(void)viewDidLoad
method.
– LunaCodeGirl
Nov 13 '14 at 20:06
add a comment |
The key thing with this is to realise that you are changing the custom view's width, rather than the UIBarButton
itself.
The code is therefore:
CGRect resizedFrame = myBarButtonItem.customView.frame;
resizedFrame.size.width = myNewWidth;
myBarButtonItem.customView.frame = resizedFrame;
You will also need to trigger the layout change:
[myNavigationBar setNeedsLayout];
All this goes without saying that the layout is being done with Auto Sizing and frames. Incursions into navigation bars with Auto Layout yielded no success. See my question Auto Layout with UINavigationBar and UIBarButtonItem.
Sorry just realised that my code is almost identical to @oscartzombie. Not intentional! I'll leave this answer as I think it's worth adding the layout and other points, in addition to explaining without reference to Interface Bulder or images specifically.
I was having issues with the size of my custom UIBarButtonItem, the solution was calling[self.navigationController.navigationBar setNeedsLayout];
in my view controller's-(void)viewDidLoad
method.
– LunaCodeGirl
Nov 13 '14 at 20:06
add a comment |
The key thing with this is to realise that you are changing the custom view's width, rather than the UIBarButton
itself.
The code is therefore:
CGRect resizedFrame = myBarButtonItem.customView.frame;
resizedFrame.size.width = myNewWidth;
myBarButtonItem.customView.frame = resizedFrame;
You will also need to trigger the layout change:
[myNavigationBar setNeedsLayout];
All this goes without saying that the layout is being done with Auto Sizing and frames. Incursions into navigation bars with Auto Layout yielded no success. See my question Auto Layout with UINavigationBar and UIBarButtonItem.
Sorry just realised that my code is almost identical to @oscartzombie. Not intentional! I'll leave this answer as I think it's worth adding the layout and other points, in addition to explaining without reference to Interface Bulder or images specifically.
The key thing with this is to realise that you are changing the custom view's width, rather than the UIBarButton
itself.
The code is therefore:
CGRect resizedFrame = myBarButtonItem.customView.frame;
resizedFrame.size.width = myNewWidth;
myBarButtonItem.customView.frame = resizedFrame;
You will also need to trigger the layout change:
[myNavigationBar setNeedsLayout];
All this goes without saying that the layout is being done with Auto Sizing and frames. Incursions into navigation bars with Auto Layout yielded no success. See my question Auto Layout with UINavigationBar and UIBarButtonItem.
Sorry just realised that my code is almost identical to @oscartzombie. Not intentional! I'll leave this answer as I think it's worth adding the layout and other points, in addition to explaining without reference to Interface Bulder or images specifically.
edited May 23 '17 at 12:17
Community♦
11
11
answered Jul 19 '13 at 13:06
Max MacLeodMax MacLeod
21.5k1081116
21.5k1081116
I was having issues with the size of my custom UIBarButtonItem, the solution was calling[self.navigationController.navigationBar setNeedsLayout];
in my view controller's-(void)viewDidLoad
method.
– LunaCodeGirl
Nov 13 '14 at 20:06
add a comment |
I was having issues with the size of my custom UIBarButtonItem, the solution was calling[self.navigationController.navigationBar setNeedsLayout];
in my view controller's-(void)viewDidLoad
method.
– LunaCodeGirl
Nov 13 '14 at 20:06
I was having issues with the size of my custom UIBarButtonItem, the solution was calling
[self.navigationController.navigationBar setNeedsLayout];
in my view controller's -(void)viewDidLoad
method.– LunaCodeGirl
Nov 13 '14 at 20:06
I was having issues with the size of my custom UIBarButtonItem, the solution was calling
[self.navigationController.navigationBar setNeedsLayout];
in my view controller's -(void)viewDidLoad
method.– LunaCodeGirl
Nov 13 '14 at 20:06
add a comment |
The following approach worked, see code to change width and height of the button and to add action item as well.
UIButton *imageButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
[imageButton setBackgroundImage:[UIImage imageNamed:@"message_button"] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(messageButtonTapped) forControlEvents:UIControlEventAllEvents];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageButton];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
Note: The target and action of the UIBarButtonItem does not apply to image view
add a comment |
The following approach worked, see code to change width and height of the button and to add action item as well.
UIButton *imageButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
[imageButton setBackgroundImage:[UIImage imageNamed:@"message_button"] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(messageButtonTapped) forControlEvents:UIControlEventAllEvents];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageButton];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
Note: The target and action of the UIBarButtonItem does not apply to image view
add a comment |
The following approach worked, see code to change width and height of the button and to add action item as well.
UIButton *imageButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
[imageButton setBackgroundImage:[UIImage imageNamed:@"message_button"] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(messageButtonTapped) forControlEvents:UIControlEventAllEvents];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageButton];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
Note: The target and action of the UIBarButtonItem does not apply to image view
The following approach worked, see code to change width and height of the button and to add action item as well.
UIButton *imageButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
[imageButton setBackgroundImage:[UIImage imageNamed:@"message_button"] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(messageButtonTapped) forControlEvents:UIControlEventAllEvents];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageButton];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
Note: The target and action of the UIBarButtonItem does not apply to image view
edited May 27 '15 at 10:01
answered May 27 '15 at 9:57
Abdullah SaeedAbdullah Saeed
2,1881922
2,1881922
add a comment |
add a comment |
None of those solutions worked for me and I figure out auto-resizing thing overrides dimensions that you wrote in code.
after creating button by UIButton way, write following code block:
[self.navigationItem.rightBarButtonItem.customView.widthAnchor constraintEqualToConstant:mWidth].active = YES;
[self.navigationItem.rightBarButtonItem.customView.heightAnchor constraintEqualToConstant:mHeight].active = YES;
add a comment |
None of those solutions worked for me and I figure out auto-resizing thing overrides dimensions that you wrote in code.
after creating button by UIButton way, write following code block:
[self.navigationItem.rightBarButtonItem.customView.widthAnchor constraintEqualToConstant:mWidth].active = YES;
[self.navigationItem.rightBarButtonItem.customView.heightAnchor constraintEqualToConstant:mHeight].active = YES;
add a comment |
None of those solutions worked for me and I figure out auto-resizing thing overrides dimensions that you wrote in code.
after creating button by UIButton way, write following code block:
[self.navigationItem.rightBarButtonItem.customView.widthAnchor constraintEqualToConstant:mWidth].active = YES;
[self.navigationItem.rightBarButtonItem.customView.heightAnchor constraintEqualToConstant:mHeight].active = YES;
None of those solutions worked for me and I figure out auto-resizing thing overrides dimensions that you wrote in code.
after creating button by UIButton way, write following code block:
[self.navigationItem.rightBarButtonItem.customView.widthAnchor constraintEqualToConstant:mWidth].active = YES;
[self.navigationItem.rightBarButtonItem.customView.heightAnchor constraintEqualToConstant:mHeight].active = YES;
answered Nov 13 '18 at 12:16
ACAkgulACAkgul
9510
9510
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10988918%2fchange-width-of-a-uibarbuttonitem-in-a-uinavigationbar%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I'm afraid I don't think there is a way to adjust the selectable area. A question though, why would you want the width to be 30 px? iOS Human Interface Guideline states that the comfortable minimum size of tappable UI elements is 44 x 44 points. developer.apple.com/library/ios/#documentation/UserExperience/…
– Abd Sani Abd Jalal
Jun 12 '12 at 0:36
Hmm, ok thanks for the link to the guidelines. It seems a bit strange that the selectable area is so wide though.
– Darren
Jun 12 '12 at 1:23
@AbdSaniAbdJalal can we change the width of
barbuttonitem
in storyboard?– aircraft
Nov 1 '16 at 3:22