I have added custom UIbarButtonItems on toolbar. Everything is created through NIB file. I see the images are stretched. My images are 20x20 and 40x40 for retina. They are png files and I ensured they dont contain any drop shadow and use anti-aliasing. Currently the toolbar looks like this.
If you see, the same images appear sharp when not added as a barbuttonItem but just as imageview. How can I fix this.
Could you try to create your UIBarButtonItems in your viewDidLoad method?
Here is how I do with a UINavigationBar and all images looks fine:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *rightButtonImage = [UIImage imageNamed:#"img1.png"];
[rightButton setImage:rightButtonImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0.0, 7.0, rightButtonImage.size.width, rightButtonImage.size.height);
[rightButton addTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *leftButtonImage = [UIImage imageNamed:"img2.png"];
[leftButton setImage:leftButtonImage forState:UIControlStateNormal];
leftButton.frame = CGRectMake(10.0, 7.0, leftButtonImage.size.width, leftButtonImage.size.height);
[leftButton addTarget:self action:#selector(method2) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
method1 and method2 are void methods: -(void)method
and to add 2 buttons on the right side, I do something like this:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *rightButtonImage = [UIImage imageNamed:#"img3"];
[rightButton setImage:rightButtonImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0.0, 7.0, rightButtonImage.size.width, rightButtonImage.size.height);
[rightButton addTarget:self action:#selector(method3) forControlEvents:UIControlEventTouchUpInside];
UIButton *middleRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *middleRightButtonImage = [UIImage imageNamed:#"img4"];
[middleRightButton setImage:middleRightButtonImage forState:UIControlStateNormal];
middleRightButton.frame = CGRectMake(0.0, 7.0, middleRightButtonImage.size.width, middleRightButtonImage.size.height);
[middleRightButton addTarget:self action:#selector(method4) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:[[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease], [[[UIBarButtonItem alloc] initWithCustomView:middleRightButton] autorelease], nil];
For UIToolbar you have to add items like this:
[toolbar setItems:[NSArray arrayWithObject:item1, item2, item3, item4, nil]];
Related
I want to change the backbutton appearance for my backbutton in my navigation bar. For that I have this piece of code.
UIImage *backButtonImage = [UIImage imageNamed:#"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.backgroundColor = [UIColor colorWithPatternImage:backButtonImage];
backbutton.titleLabel.text = #"back";
backbutton.titleLabel.textColor= [UIColor colorWithRed:50/255.0
green:158/255.0
blue:218/255.0
alpha:1.0];
backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[backbutton addTarget:self action:#selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = back;
But still I get the standard back button. Any help ?
Put your code to customize back button before pushing that viewController to the stack. And set BackBarButtonItem , not leftBarButtonItem.
UIImage *image = [UIImage imageNamed:#"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[backbutton setImage:image forState:UIControlStateNormal];
backbutton.titleLabel.textColor = [UIColor blueColor];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[[self navigationItem] setBackBarButtonItem: back];
[self.navigationController pushViewController:yourViewControllerObject animated:YES];
using bellow code for your requirement..
- (void)viewDidLoad
{
///write your code
UIImage *backButtonImage = [UIImage imageNamed:#"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
//[backbutton setImage:backButtonImage forState:UIControlStateNormal];//this line for set only image on button
[backbutton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[backbutton setTitle:#"Back" forState:UIControlStateNormal];
backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[backbutton addTarget:self action:#selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = back;
}
when back button clicked bellow method call and you will go to previous viewcontroller
-(void)back_Clicked{
[self.navigationController popViewControllerAnimated:YES];
}
Use
self.navigationItem.leftBarButtonItem = back;
Instead of
self.navigationController.navigationItem.leftBarButtonItem = back;
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:[self buttonWithImage:#"backButton" heighlightImageName:#"backButton" buttonFrame:CGRectMake(2, 1, 30, 30) selectorName:#selector(buttonBackClicked:) target:self]]];
- (UIButton*)buttonWithImage:(NSString*)normalImageName heighlightImageName:(NSString*)heighlightImageName buttonFrame:(CGRect)buttonFrame selectorName:(SEL)selectorName target:(id)target
{
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[tempButton setFrame:buttonFrame];
[tempButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
[tempButton setImage:[self getImageFromResource:normalImageName] forState:UIControlStateNormal];
[tempButton setImage:[self getImageFromResource:heighlightImageName] forState:UIControlStateHighlighted];
return tempButton;
}
- (UIImage*)getImageFromResource:(NSString*)imageName
{
return [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:#"png"]];
}
IButton *favButton = [[UIButton alloc] init];
[favButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[favButton addTarget:self action:#selector(favouriteButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithCustomView:favButton];
self.navigationItem.rightBarButtonItem = button;
[button release];
[favButton release];
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 a UIToolbar.
If I create a UIBarButton like this, it works:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:iOB style:UIBarButtonItemStylePlain target:self action:#selector(myStuff)];
[button setTitle:#"Hi"];
If I create the UIBarButton like this, cause I want it to be colored:
UIImageView *vUP = [[UIImageView alloc] initWithImage:iUP];
UIBarButtonItem *UPButton = [[UIBarButtonItem alloc] initWithCustomView:vUP];
[UPButton setAction:#selector(anotherStuff)];
[UPButton setTitle:#"Hi";
[UPButton setTarget:self];
[vUP release];
The button shows, WITHOUT title and will doesn't respond to any touch...
am I missing something?
UIImage* image3 = [UIImage imageNamed:#"plain_btn-75-30.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(backButtonPress:)forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
[someButton setTitle:#"Back" forState:UIControlStateNormal];
someButton.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:13];
[someButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UPButton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
If you want image and title both then image must be set as a background image with title.
Use a UIButton instead of the UIImageView as a UIImageView does not respond to touch events.
You will, under no circumstance get a title as you provided a custom view.
I have a barbuttonitem that made programmatically above a navigation controller. I want to display it's title and background also a highlighted effect when i press that bar button.
Here's the code I use :
NSArray *segmentText = [segmentTextMutable copy];
UIImage *image = [[[UIImage alloc] init] autorelease];
image = [UIImage imageNamed:#"bunga.jpg"];
_docSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentText];
_docSegmentedControl.selectedSegmentIndex = 0;
_docSegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_docSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
[_docSegmentedControl addTarget:self action:#selector(docSegmentAction:) forControlEvents:UIControlEventValueChanged];
[_docSegmentedControl setBackgroundColor:[UIColor colorWithPatternImage:image]];
UIView *barBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"button.png"];
UIImage *buttonPressedImage = [UIImage imageNamed:#"buttonPressed.png"];
[[barButton titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0]];
[barButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[barButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[barButton setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.7] forState:UIControlStateNormal];
[barButton setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];
[[barButton titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];
CGRect buttonFrame = CGRectMake(0, 0, 110.0, 40);
//buttonFrame.size.width = 110.0;
//buttonFrame.size.height = buttonFrame.size.height;
[barButton setFrame:buttonFrame];
[barButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[barButton setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];
[barButton setTitle:docSegmentFileName forState:UIControlStateNormal];
[barButton addTarget:self action:#selector(docSegmentAction:) forControlEvents:UIControlEventTouchUpInside];
[barBackground addSubview:barButton];
UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:_docSegmentedControl];
self.navItem.leftBarButtonItem = segmentItem;
self.navItem.leftBarButtonItem.title = #"";
[self.navItem.leftBarButtonItem setCustomView:barBackground];
Unfortunately, this doesn't work. Instead of showing the UIBarButtonItem, it simply vanishes (it becomes 100% transparant). When I leave out the setCustomView method, the UIBarButtonItem appears, but is not customized. How can i solve this problem?
thank's...
You are removing the UISegmentedControl from the UIBarButtonItem when you call [self.navItem.leftBarButtonItem setCustomView:barBackground];. You need to modify the UISegmentedControl not the UIBarButtonItem.
I have a Tab bar application. and in one of the tab bars I have a navigation contorller.
Im trying to set a UIButton with an image as the right button the navigation bar.
UIButton *refreshButton = [[UIButton alloc] init];
[refreshButton setImage:[UIImage imageNamed:#"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refreshSection) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:refreshButton];
//[rightButton setImage:[UIImage imageNamed:#"refresh_icon.png"]]; << DOESN'T WORK EITHER
self.navigationItem.rightBarButtonItem = rightButton;
[refreshButton release];
[rightButton release];
but the image doesn't show up. I get an invisible button.
EDIT:
I want the button style to be plain or background to be transparent, so that only the image shows. Hence I am using a UIbutton instead of using a UIBarButton directly.
You don't have to create a UIButton and use it as custom view in the UIBarButtonItem. You can create a UIBarButtonItem directly with an image:
UIImage *myImage = [UIImage imageNamed:#"myImage.png"];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStyleBordered target:self action:#selector(refreshSection)];
self.navigationItem.rightBarButtonItem = button;
[button release];
--
Updated, according to comments
Use a UIImageView as custom view in the UIBarButtonItem:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.rightBarButtonItem = rightButton;
[imageView release];
[rightButton release];
--
Updated
Tried the last approach and while this does visually look like you want it to, I can't seem to get it to handle taps. Although the target and action has been set.
--
Final update
I've gotten your initial code in the question up & running. The reason the image is not showing, is because you haven't set the frame of the button. Once you set the frame, the image is shown in the navigation bar.
Here's my snippet I used & tested:
UIImage *myImage = [UIImage imageNamed:#"paw.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
[myButton release];
Note: I have set the showsTouchWhenHighlighted property of the UIButton to visually hightlight the button when it is pressed.
I ended up doing this
UIButton *refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(292, 9, 27, 27)];
[refreshButton setImage:[UIImage imageNamed:#"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refresh) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:refreshButton];
[refreshButton release];
It works fine now but I am not really sure if this is the right way of doing it.