I'm trying to set image to rightBarButtonItem.
I tried to do this 2 ways:
/* first way */
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,105,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"image.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(rightBarButtonItemTapped) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
/* second way */
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image"]
style:UIBarButtonItemStyleBordered
target:self
action:#selector(rightBarButtonItemTapped)];
P.S. When I tap right side of navigation bar I see that rightBarButtonItemTapped method invoked.
You can set an image to a bar button using the attribute inspector.
Related
I have a navigation controller application, and I need to set the custom action for the navigation back bar button. Tried some workarounds and not yet to find a solution.
Tried
UIBarButtonItem *backBarItem = self.navigationItem.leftBarButtonItem;
backBarItem.target = self;
backBarItem.action = #selector(popToHomeViewController);
Result : No effect. Back button pops to just previous viewController in navigation stack
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:backBarItem.title style:backBarItem.style target:self action:#selector(popViewController)];
self.navigationItem.leftBarButtonItem = customBarItem;
Result : No effect. Back button pops to just previous viewController in navigation stack
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:#"back" style:backBarItem.style target:self action:#selector(popViewController)];
self.navigationItem.leftBarButtonItem = customBarItem;
Result:Now my selector got invoked perfectly and navigated to desired viewController. Here the issue is that the back button not as like as the native back button. It is not having the bold "<" character as I have not mentioned it. If added < character it needs to be changed for ios 6 compatibility.
Any better solution to ensure ios 6 and ios 7 compatible navigation back button with custom selector?
Try this simple example will help you..
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
}
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
Make sure you have an button image with the size of a navigation bar back button in your resource folder with name back.png
Feel free if any other assistance is required.
Happy Coding!!!!!
Try this
viewController.navigationItem.hidesBackButton = YES;
//set custom image to button if needed
UIImage *backButtonImage = [UIImage imageNamed:#"back"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:backButtonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[button addTarget:viewController action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height)];
[backButtonView addSubview:button];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
viewController.navigationItem.leftBarButtonItem = customBarItem;
and in back method you can customise
- (void)back {
//Your code
}
I have created view with barbutton as follows
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"settingsicon.png"] style:UIBarButtonItemStylePlain target:self action:#selector(settingsButtonClicked:)];
It is comming like this
but I want it as
how can I remove the border of the button?
Use this :
UIBarButtonItem *barButton= [[UIBarButtonItem alloc] initWithCustomView:customButton];
You can initialize a button with whatever view you would like. You can make a button with any (acceptable) view inside it.
eg.
// GRAPHICAL BUTTON FRAMEWORK
UIButton* btton = [UIButton buttonWithType:UIButtonTypeCustom];
[btton setFrame:CGRectMake(0, 0, 30, 30)];
[btton addTarget:self action:#selector(SOMEROUTINE) forControlEvents:UIControlEventTouchUpInside];
[btton setImage:[UIImage imageNamed:#"SOME IMAGE"] forState:UIControlStateNormal];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:btton];
Hope it helps you.
Another way is to remove button's background image:
// portrait
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
// landscape
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsLandscapePhone];
I am using these icons as bar button item in a toolbar
http://stopiransnukes.org/images/home_icon.png
and
http://www.bladmuziekplus.nl/shop/files/images/icon_home.png
But background image is converted to white automatically even if its black.
I tried setting image background via code but still its turning into white.
Manually : I have Dragged+Dropped the above image to the project.I have selected the bar button item and on 4th option on the right bar (shield icon) select different tint and under bar button drop-down image is set to the above image.
Programmatically :
[self.homeButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor blackColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
[[self.homeButton] setImage:[UIImage imageNamed:#"home_icon.png"]];
How do I fix it ?
Thanks in Advance.
.
This is a step by step process to add a image into UIButton and add that UIButton as a custom view inside the UIBarButtonItem in the storyboard itself.. I will add some images for you to understand this totally..
step 1: Drag and drop the toolbar in storyboard view
step 2: Drag and drop the UIButton in your view
step 3: Drag and drop the image in your UIButton
step 4: Drag and drop the UIButton in the toolbar and now you can in the right side that it is added inside of the UIBarButtonItem. now you just have to connect an action method for your button.
viewDidLoad
[super viewDidLoad];
//uıbarButtonItem firstButtonItem,secondButtonItem
UIImage* firstButtonImage = [UIImage imageNamed:#"firstImageName"];;
CGRect frameimg = CGRectMake(0, 0, 30, 30);
UIButton * someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:firstButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(goToUserProfile)
forControlEvents:UIControlEventTouchUpInside];
self.firstButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];
UIImage* secondButtonImage = [UIImage imageNamed:#"secondImageName"];
frameimg = CGRectMake(0, 0, 30, 30);
someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:secondButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(likePhoto)
forControlEvents:UIControlEventTouchUpInside];
[someButton setTag:1];
self.secondButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];
[self.toolbar setItems:[self addItemsToToolbar]];
AddItemsToToolbar Method
- (NSMutableArray *)addItemsToToolbar{
NSMutableArray *items = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[items addObject:flexSpace];
[items addObject:self.firstButtonItem];
[items addObject:flexSpace];
[items addObject:self.secondButtonItem];
return items;
}
This is the normal behaviour, you can read about it in the Human Interface Guidelines.
http://developer.apple.com/library/ios/documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html#//apple_ref/doc/uid/TP40006556-CH14-SW8
If you want to change that you need to write your own subclasses.
UIImage* infoButtonImage = [UIImage imageNamed:#"info_button.png"];;
CGRect frameimg = CGRectMake(0, 0, infoButtonImage.size.width, infoButtonImage.size.height);
UIButton * someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:infoButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(actionOfButton)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:someButton];
This code is going to solve your problem
How can I remove dafult button to place my custom button in navbar? Problem at the moment is that my custom button is over default button.
Please take a look at the screenshot and it will be more clear.
I'm getting this with following code:
- (void)viewDidLoad
{
UIImage *menuImage = [UIImage imageNamed:#"barMenuButton.png"];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:menuImage style:UIBarButtonItemStylePlain target:self action:#selector(ShowLeftMenu:)];
[self.navigationItem setRightBarButtonItem:addButton];
}
This will fix your problem:
UIImage *menuImage = [UIImage imageNamed:#"barMenuButton.png"];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 100, 40);
[button setImage:menuImage forState:UIControlStateNormal];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[addButton setAction:#selector(ShowLeftMenu:)];
[self.navigationItem setRightBarButtonItem:addButton];
You'll have to create a custom view and add that to the bar button item, as described in this article. This article also provides some details about creating custom UIButtons.
I'm developing an application where I use UINavigationController. I would like to set the image for a back button. I do this using the code below.
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backArrow.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
But the image appears only in the middle of the back button. How can I set the image to span the whole back button?
For back button in UINavigationBar, if you add image it will show an image in center of the back buttobn only. You cannot add image in that too. So instead of doing that, hide your backbutton and add this code in your viewDidload Method to add a image for it and looks like what you need.
self.navigationItem.hidesBackButton = YES;
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"back_normal"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"back_pressed"] forState:UIControlStateSelected];
[button addTarget: nil action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 50, 30);
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: button];