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
Related
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];
Below code is working fine in my application. But that leftbarbuttonitem is invisible. And I can't set background image to leftBarButtonItem. I did some thing wrong, but I don't know what is the mistake? Is it possible to make visible and set background image?
UIImageView *NavigationBarImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
NavigationBarImage.image=[UIImage imageNamed:#"mapbutton.png"];
[self.navigationController.navigationBar addSubview:NavigationBarImage];
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered target:self action:#selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
-(void)goBack
{
[self.navigationController popViewControllerAnimated:YES];
}
If you want to add a backgroundImage to your navigation item you have to add a custom button, like bellow:
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
tempButton.frame = CGRectMake(0, 0, 50, 30);
tempButton.showsTouchWhenHighlighted = YES;
tempButton.backgroundColor = [UIColor clearColor];
[tempButton setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[tempButton setTitle:#"Back" forState:UIControlStateNormal];
[tempButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc]initWithCustomView:tempButton];
self.navigationItem.leftBarButtonItem = backBtn;
And to add a background Image to navigation bar you can use the following code
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"header.png"] forBarMetrics:UIBarMetricsDefault];
UIButton *home = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *homeImage = [UIImage imageNamed:#"YOUR_IMAGE.png"];
[home setBackgroundImage:homeImage forState:UIControlStateNormal];
[home addTarget:self action:#selector(your_Action)
forControlEvents:UIControlEventTouchUpInside];
home.frame = CGRectMake(0, 0, 69, 26);
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:home];
[[self navigationItem] setLeftBarButtonItem:button2];
[button2 release];
button2 = nil;
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];
I have this code that changes the back button of my UINavigationBar
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:#"backag.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"selback.png"] forState:UIControlStateHighlighted];
button.adjustsImageWhenDisabled = NO;
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 30, 30);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
// Cleanup
[customBarItem release];
If I put it in the viewDidLoad method it works fine. However, when I load the next view the old style button shows up. To fix this I tried putting this code in the next view's viewDidLoad method, but then no button is visible.
Any ideas as to what might be causing this?
Thanks!
Apply same code in
-(void)viewDidAppear:(BOOL)animated
{
}
I used tis code in both view and it's worked fine...
// add following code in your both view's viewDidLoad method...
UIButton *leftButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton1 setImage:[UIImage imageNamed:#"Viewone.png"] forState:UIControlStateNormal];
leftButton1.frame = CGRectMake(0, 0, 30, 30);
[leftButton1 addTarget:self action:#selector(yourclickevent) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton1];
[leftButton1 release];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:[UIImage imageNamed:#"ViewSecond.png"] forState:UIControlStateNormal];
leftButton.frame = CGRectMake(0, 0, 30, 30);
[leftButton addTarget:self action:#selector(yourclickevent) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
[leftButton release];
Hope,this will help you...enjoy..
You need to ensure you set the hidesBackButton property on the navigationItem:
// Setup custom back button
self.navigationItem.hidesBackButton = YES;
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:.