iPhone . UIToolbar not working with some buttons - iphone

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.

Related

UIbarbuttonItem image looks stretched/thicker

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]];

How to make visible a leftBarButtonItem created programmatically and set backgroundimage

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;

how to display the go back style UIBarButtonItem programmatically

When One of my app navigate from one controller to another, the 'go back' UIBarButtonItem disappear, so I wrote codes:
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStyleDone
target:self
action:#selector(barButtonItemPressed:)];
self.navigationItem.leftBarButtonItem = barButton1;
[barButton1 setImage:[UIImage imageNamed:#"fdj.png"]];
[barButton1 release];
It works, but it display as
but I prefer the style (like standard go back barbuttonitem
Is it possible?
Welcome any comment
You should edit the navigationItem of the parent view controller:
You can place this in the viewDidLoad of the previous view controller:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
self.navigationItem.backBarButtonItem.image = [UIImage imageNamed:#"fdj.png"];
here is the code how you can do this
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0, 0, 55, 36);
[btnBack setImage:[UIImage imageNamed:#"btnBack"] forState:UIControlStateNormal];
[btnBack setImage:[UIImage imageNamed:#"btnBack"] forState:UIControlStateSelected];
[btnBack addTarget:self action:#selector(onClickBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = item4;
just make image and set it in uibutton, it's simple way to do this.
you can set any image you want.
hope this may help you what you want.
In the parent view controller's init method, set
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithImage: [UIimage imageNamed: #"fdj.png"]
style: UIBarButtonItemStyleBordered
target: nil
action: nil] autorelease];
This is what I'm using in an older project of mine (and yes, I know that stretchableImageWithLeftCapWidth is deprecated).
#implementation UIViewController (UIView_ExtenderClass) // this is a category, obviously
...
-(void)setLeftButton:(id)theTarget navItem:(UINavigationItem *)myNavItem action:(SEL)myAction title:(NSString *)myTitle;
{
CGSize titleWidth = [myTitle sizeWithFont:[UIFont boldSystemFontOfSize:14.0]];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, titleWidth.width + 20, 32)];
[button setTitle: [NSString stringWithFormat:#" %#", myTitle] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
[button addTarget:theTarget action:myAction forControlEvents:UIControlEventTouchUpInside];
UIImage *imgBack = [UIImage imageNamed:#"32_left_nav_bar.png"];
UIImage *imgBackStretched = [imgBack stretchableImageWithLeftCapWidth:15 topCapHeight:0];
UIImage *imgBackSelected = [UIImage imageNamed:#"32_left_nav_bar_selected.png"];
UIImage *imgBackSelectedStretched = [imgBackSelected stretchableImageWithLeftCapWidth:15 topCapHeight:0];
[button setBackgroundImage:imgBackStretched forState:UIControlStateNormal];
[button setBackgroundImage:imgBackSelectedStretched forState:UIControlStateHighlighted];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[buttonItem setTarget:theTarget];
myNavItem.leftBarButtonItem = nil;
myNavItem.leftBarButtonItem = buttonItem;
[imgBack release];
[imgBackSelected release];
[button release];
[buttonItem release];
}
Call it in the viewDidLoad of the second controller, like this:
[self setLeftButton:self navItem:self.navigationItem action:#selector(yourActionHere) title:#"Back"]
The image I'm using is here: http://i49.tinypic.com/6qfzfs.png

How to give style in left nav bar button in iphone

I added 2 button with images in top left navigation bar. But I am getting problem in its style. Here is my screenshot of button. And this is code of button which I have written in application.
UIView *viewButtons = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 32)];
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,40,35);
[button1 setBackgroundImage:[UIImage imageNamed: #"remainder.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(sortByremainder) forControlEvents:UIControlEventTouchUpInside];
[viewButtons addSubview:button1];
UIButton *button2 = [[UIButton alloc] init];
button2.frame=CGRectMake(45,0,60,35);
[button2 setBackgroundImage:[UIImage imageNamed: #"like_symbol.jpg"] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(sortByMostFaved) forControlEvents:UIControlEventTouchUpInside];
[viewButtons addSubview:button1];
[viewButtons addSubview:button2];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:viewButtons];
[viewButtons release];
But i want my button style should be like this image. I want to keep images in button but below screen is more specific with button. It has border and convex area.
For that you need to use grayscale images and that would look better. See example like attached.
You need to use UIBarButtonItem and set this type of images in that and that assign that UIBarButtonItem to self.navigationItem.leftBarButtonItem.
Also, Use BarButtonItemStyle Border like below so you will get as you want.
UIBarButtonItem *btnSetting = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"setting.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(setting:)];
self.navigationItem.rightBarButtonItem = btnSetting;

combining UISegmentedControl and UIBarButtonItem in navigation controller to customized barbuttonitem background image

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.