Following is a code to display a button to the toolbar.
UIButton myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *myImage = [UIImage imageNamed:#"img1.png"];
[myButton setBackgroundImage:myImage forState:UIControlStateNormal];
UIBarButtonItem *myToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
Nothing gets displayed on the toolbar. Please tell me what is wrong.
[myToolbar setItems:[NSArray arrayWithObject:myToolbarButton]]; // might help
OR if it's a navigation bar
self.navigationItem.rightBarButtonItem = myToolbarButton;
Also you should set the frame for the custom button, something like this:
UIButton *mybutton = [[UIButton alloc] initWithFrame:CGRectMake:0,0,200,50];
Then if you want to add a selector/callback:
[myButton addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = myButtonItem; // and change for the right button.
Where self is a UIViewController.
You forgot to add this button to the toolbar!
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myToolbarButton,nil]; // you can more buttons here
[aToolbar setItems:toolbarItems animated:NO]; // change to YES if you want to have nice animation
[toolbarItems release];
If you want to add a button to navigation bar, use this:
[self.navigationItem setRightBarButtonItem:myToolbarButton]; // or setLeft...
Also, you should remember to set the frame of myButton:
[myButton setFrame:CGRectMake(0, 0, 30, 32)];
Related
I know viewcontroller's navigation item's backBarButtonItem gets displayed when another view controller is pushed on stack and this is 2nd viewcontroller from top.
I have viewcontroller A which have following in viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = NO;
[self.navigationItem setBackBarButtonItem:[UIBarButtonItem itemWithImageNamed:#"ic_header_slide" selectedImage:nil target:nil action:nil]];
}
When I push viewcontroller B, this custom back button is not getting displayed, instead I see default back button which iOS creates.
A extends UITableViewController and B extends UIViewController. I am not setting leftBarButtonItem, leftBarButtonItems, rightBarButtonItem, rightBarButtonItems in any of these navigationItem.
EDIT
I have read about setting leftBarButtonItems. setting leftbarbuttonitems on B works. but I think setting backBarButtonItem on A is correct way of doing that. It is also mentioned in documentation but not working in my case. I want to ask whether there is bug in backBarButtonItem or I have some misunderstanding the way it works and I am not doing it correctly.
To hide the default back button of the navigation bar use,
self.navigationItem.hidesBackButton=TRUE;
Also use the following method to add the custom BarButtons,
- (NSArray*)getLeftNavButtons:(NSString*)image andTarget:(id)target andFrame:(CGRect)frame andSpace:(int)fixedSpace
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = frame;
button.clipsToBounds = YES;
[button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[button addTarget:target action:#selector(leftNavBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:button];
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7"))
{
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = fixedSpace;
return #[negativeSpacer,barButton];
}
else{
return #[barButton];
}
return #[barButton];
}
Just override the default
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *back = [[UIBarButtonItem alloc]init];
back.title = #"Pick Me";
back.image = #"Your image";
[self.navigationItem setLeftBarButtonItem:back];
Set Right Bar Button Item
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]init];
UIImage *img1=[UIImage imageNamed:#"edit"];
CGRect frameimg1 = CGRectMake(0, 0, img1.size.width, img1.size.height);
UIButton *signOut=[[UIButton alloc]initWithFrame:frameimg1];
[signOut setBackgroundImage:img1 forState:UIControlStateNormal];
[signOut addTarget:self action:#selector(btnEditClicked:)
forControlEvents:UIControlEventTouchUpInside];
// [signOut setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:signOut];
self.navigationItem.rightBarButtonItem=barButton;
Set Left Bar Button Item
UIImage *img11=[UIImage imageNamed:#"home"];
CGRect frameimg11 = CGRectMake(0, 0, img11.size.width, img11.size.height);
UIButton *signOut1=[[UIButton alloc]initWithFrame:frameimg11];
[signOut1 setBackgroundImage:img11 forState:UIControlStateNormal];
[signOut1 addTarget:self action:#selector(showLeftMenuPressed:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton1=[[UIBarButtonItem alloc]initWithCustomView:signOut1];
self.navigationItem.leftBarButtonItem=barButton1;
self.navigationController.navigationBar.barTintColor=ColorNav;
self.navigationController.navigationBar.translucent=FALSE;
Set Navigation Title & Color
self.title = titletext;
[[[self navigationController] navigationBar]setTitleTextAttributes:#{NSForegroundColorAttributeName: textColor}];
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 UINavigarionController and I want to add custom background and logo and left button to the navigation bar I could add background but my logo and button not appear, would you please help me , Thanks in advance!
Your code worked and i am getting the logo and button but with one change
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"menu.png"] forState:UIControlStateNormal];
//[button addTarget:self action:#selector(parametres:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 25, 25)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = rightButton;
UIImageView *navigationImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 120, 30)];
navigationImage.image=[UIImage imageNamed:#"menu.png"];
UIImageView *workaroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
[workaroundImageView addSubview:navigationImage];
self.navigationItem.titleView= workaroundImageView;
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"menu.png"]];
Just specify the image type i.e not menu as menu.png and just add it for your navigationItem by setting UINavigationControllerDelegate to you class as above good luck and please share your results
barButton can't be set to be used in whole app.
Each ViewController provides own barButtonItems and titleView to the navigationItem so you have to move the code of setting logo and button to the rootViewController class
You cannot create in your AppDelegate
Open your ViewController.m
- (void)viewDidLoad
{
self.navigationItem.title=#"Title"; //Set the title
[self CustomButton];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(void) CustomButton
{
UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];//create a button
but.frame=CGRectMake(290, 4, 40, 40);// set the button frame size
[but setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[but addTarget:self action:#selector(call here) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:but];
self.navigationItem.rightBarButtonItem = btn;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}
Hope its useful for you:)
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.