UIBarButtonItem with image and text - iphone

I have to create a UIBarButton on the having a globe and a text "Map" written on it.
What I have done:
I have added a UIButton with the image of the globe as backgroundImage.
Set the text on the button.
Then created a UIBarButton initWithCustomView from the above button.
Given it style as Bordered.
Result:
Now what i got is a plane button with the globe image stretched on that which is overlapping with the Text in it.
I have searched for this a lot but i only found links which show the image in background and the text above the button. Nothing like the image and text together.
Can anyone help me in creating it the way it is shown in the attachment.

Try this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"Title" forState:UIControlStateNormal];
UIImage *butImage_on = [[UIImage imageNamed:#"closebutton_on.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
UIImage *butImage_over = [[UIImage imageNamed:#"closebutton_over.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[button setBackgroundImage:butImage_on forState:UIControlStateNormal];
[button setBackgroundImage:butImage_over forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"globe.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(closeProfilePage) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 61, 30);
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 2)];
[button setImageEdgeInsets:UIEdgeInsetsMake(30,
20,
0,
20)];
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
self.navigationItem.leftBarButtonItem = backButton;
Note: you need to manage insets according to your requirements.

Use image with #2x. if your graphices are high resolution. according to 640 X 960 px;:-
UIView *parentView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 6,51, 44)];
UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, 51, 30)];
[btnBack setBackgroundImage:[UIImage imageNamed:#"back#2x.png"] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[parentView1 addSubview:btnBack];
[btnBack release];
UIBarButtonItem *homeBarButtomItem2 = [[UIBarButtonItem alloc] initWithCustomView:parentView1];
[parentView1 release];
[self.navigationItem setLeftBarButtonItem:homeBarButtomItem2];
[homeBarButtomItem2 release];

Related

Back button on custom navigation bar

I am trying to develop an app with a navigation bar of custom size. (100px) In the navigation bar, I also want to include a back button. I have added both of them using the child view, but the back button is not working:
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"menueButton.png"];
UIImage *backBtnImagePressed = [UIImage imageNamed:#"menueButton.png"];
backBtn.exclusiveTouch = YES;
[backBtn addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn setBackgroundImage:backBtnImagePressed forState:UIControlStateHighlighted];
backBtn.frame = CGRectMake(0, -40, 35, 32);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, -40, 35 , 32)];
[backButtonView addSubview:backBtn];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
self.navigationItem.leftBarButtonItem = backButton;
My navigation bar size is 100 pixels tall, and when I use button position (0,0,35,32) it will work correctly. But in this case, the button is being displayed much lower than intended. What am I doing wrong?
self.btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnBack setImage:[UIImage imageNamed:#"back_active.png"] forState:UIControlStateNormal];
[self.btnBack addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
self.btnBack.frame = CGRectMake(5, 9, 50, 30);
[self.navigationController.navigationBar addSubview:self.btnBack];
check this link also.
Update:for back button
-(void)back
{
[self.btnBack removeFromSuperview];
[self.navigationController popViewControllerAnimated animated:NO];
}
Try allocing backButtonView like this
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 , 32)];
Change : orgin of view to (0,0)
Edit
Try
backBtn.frame = CGRectMake(0, 0, 35, 32);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, -40, 35 , 32)];

Making custom back button for navigation bar

I have made at least 10 buttons for my navigation bar, but it never seems to work right.
The rounded edges get pixelated. I cant have that in an app, so can anyone tell me how to make a good icon that looks like an apple one? Also what is the proper size? The code in the app for the button is
UIButton *backbtn = [UIButton buttonWithType:UIButtonTypeCustom];
backbtn.frame = CGRectMake(0, 0, 55, 30);
[backbtn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[backbtn addTarget:self action:#selector(goBackOne) forControlEvents:UIControlEventTouchUpInside]; forState:UIControlStateNormal ];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbtn];//set new button
self.navigationItem.hidesBackButton = YES;//hide original back button
Try this code:
UIImage *backButtonImage = [UIImage imageNamed:#"backButton.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBackBarItem;
and, the back method:
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
The code above just set the image for normal state. You can also set highlighted state for a better appearance. Add some codes Like:
UIImage *backButtonImageHighlighted = [UIImage imageNamed:#"backButtonHighlighted.png"];
[backButton setImage:backButtonImageHighlighted forState:UIControlStateHighlighted];

Move left barbutton item more to the right

I have a leftbar button item. But I want it to move it more to the right. At the moment it sticks to the left hand side. Any body has an idea how I can achieve this?
Here is my code for the button itself.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"task_status.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(changeStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(80, 0,100, 18)];
self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Hope anyone kan help me with this.
Kind regards
this is what I have done it worked for me
UIImage *image = [UIImage imageNamed:#"btn_bar_back.png"] ;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10.0, -15.0, image.size.width, image.size.height);//set your button frame
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitle:#"BACK" forState:UIControlStateNormal];
button.contentEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);// if want to move text more towards right
[button addTarget:self action:#selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width+10, 50)];
button.center = CGPointMake(view.center.x, view.center.y);
[view addSubview:button];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:view] autorelease];
[view release];
Why not Adding Directly rightbar Button?
self.tabBarController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(changeStatus:)] autorelease];

Change UIButton highlighted image in UINavigationBar

I'm trying to change a UIImage in a UIButton, when highlighted. The UIButton is situated in a UINavigationController.
I have the following code:
UIView *containingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];
UIButton *barUIButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barUIButton setImage:[UIImage imageNamed:#"Add.png"] forState:UIControlStateNormal];
barUIButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
barUIButton.frame = CGRectMake(-9, 0, 28, 28);
[barUIButton addTarget:self action:#selector(addButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[barUIButton setImage:[UIImage imageNamed:#"AddHighlighted.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
[containingView addSubview:barUIButton];
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
self.navigationItem.rightBarButtonItem = containingBarButton;
Instead of the new image showing when highlighted, there is just a black shadow around the existing image.
Why is this?
It looks like the UIButton instance isn't getting highlighted or selected on the touch down event. This is probably because UIBarButtonItem instances don't act like normal buttons; in fact, they're not even UIButton subclasses.
There is a workaround. If you keep a reference to your UIButton in an instance variable, you can add code to change the button's image:
[barUIButton addTarget:self action:#selector(pressDown:) forControlEvents:UIControlEventTouchDown];
[barUIButton addTarget:self action:#selector(pressUp:) forControlEvents:UIControlEventTouchUp];
In pressDown: and pressUp:, you can set the
-(void)pressDown:(id)sender
{
[barUIButton setImage:[UIImage imageNamed:#"Add.png"] forState:UIControlStateNormal];
}
And similarly for pressUp:.

changing UIBarButtonItem UIButton's image

I have a UIBarButtonItem with a custom view of a UIbutton with an image, I want to have a different UIImage for different orientation, so here's my code:
UIButton * backButton = (UIButton *) ((UIBarButtonItem *) self.navBar_.topItem.leftBarButtonItem).customView;
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) && IS_IPAD) {
[backButton setBackgroundImage:[UIImage imageNamed:#"back-landscape.png"] forState:UIControlStateNormal];
[backButton setFrame: CGRectMake(0, 0, 46, 35)];
} else {
[backButton setBackgroundImage:[UIImage imageNamed:#"back-reading.png"] forState:UIControlStateNormal];
[backButton setFrame: CGRectMake(0, 0, 23, 19)];
}
However this doesn't change the UIButton image, why is this?
Try this:-
UIButton * backButton ;
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) && IS_IPAD)
{
backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 66.0f, 36.0f)];
[backButton setImage:[UIImage imageNamed:#"back-landscape.png"] forState:UIControlStateNormal];
}
else
{
backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 23, 19)];
[backButton setImage:[UIImage imageNamed:#"back-reading.png"] forState:UIControlStateNormal];
[backButton setFrame: CGRectMake(0, 0, 23, 19)];
}
[backButton addTarget:self action:#selector(goToHome) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *HomeButton = [[UIBarButtonItem alloc] initWithCustomView: backButton];
[self.navigationItem setLeftBarButtonItem:HomeButton];
You should notify that you say it is a UIBarButtonItem , but you use UIButton in code.
From ios5.0, you can use UIAppearence Protocol to change UIBarButtonItem background image, like:
[[UIBarButtonItem appearance] setBackgroundImage: forState: barMetrics:]
You can also use elppa's method, but notice you can directly use UIImageView for the customView parameter.
You could use the new proxy class with +appereance method check the DOC, but is only for iOS5. For earlier support you can update the image while the view is rotating the view controller method -willRotateToInterfaceOrientation.
Ciao
Create a UIBarButton using a custom UIButton:
_optionsButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_optionsButton setImage:[UIImage imageNamed:#"menu2.png"] forState:UIControlStateNormal];
[_optionsButton addTarget:self action:#selector(optionsButtonPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * optionsItem = [[UIBarButtonItem alloc] initWithCustomView:_optionsButton];
Then set a background image as you would normally for that UIButton (instead of trying to change the image for the UIBarButton).
[_optionsButton setImage:[UIImage imageNamed:#"keyboard.png"] forState:UIControlStateNormal];
This worked great for me
Note: When I was going through this I was changing the picture depending on what the current picture was. I found I had to use:
if ([[_optionsButton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed: #"keyboard.png"]]) {
// Code for the certain button function
}
to get it to work