How to implement image and text for bar button item - ios

The iPhone's default calendar program shows the "<" and text at the same time in the navigator bar.
I want to have image and text simultaneously as well.
How is this implemented in Xcode?

Create a custom button
UIButton *barBt =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
[barBt setImage:[UIImage imageNamed:#"my_image.png"] forState:UIControlStateNormal];
[barBt setTitle:#"MyTitle" forState:UIControlStateNormal];
[barBt addTarget:self action: #selector(pressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc]init];
[barItem setCustomView:barBt];
self.navigationItem.leftBarButtonItem = barItem;

You can only have image or text inside the UIBarbuttonItem, the < is added by iOS by default as a back button for the navigation hierarchy.
You have to design the < button in the image itself if you want both image and the < in the barButton.

Related

Customize Navigation Bar in ios

I used Navigation Controller & navigation Bar in StroryBoard.
I want to customize navigation Bar for one view as follow for right side of navigation bar.
My tried code
UIButton *btn_list=[UIButton buttonWithType:UIButtonTypeCustom];
btn_list.frame=CGRectMake(0, 0, 60, 30);
[btn_list setTitle:#"Liste" forState:UIControlStateNormal];
[btn_list setBackgroundImage:[UIImage imageNamed:#"red-left.png"] forState:UIControlStateNormal];
[btn_list setBackgroundImage:[UIImage imageNamed:#"black-left.png"] forState:UIControlStateSelected];
UIBarButtonItem *list_bar=[[UIBarButtonItem alloc]initWithCustomView:btn_list];
UIButton *btn_map=[UIButton buttonWithType:UIButtonTypeCustom];
btn_map.frame=CGRectMake(0, 0, 60, 30);
[btn_map setTitle:#"Karte" forState:UIControlStateNormal];
[btn_map setBackgroundImage:[UIImage imageNamed:#"red-right.png"] forState:UIControlStateNormal];
[btn_list setBackgroundImage:[UIImage imageNamed:#"black-right.png"] forState:UIControlStateSelected];
UIBarButtonItem *map_bar=[[UIBarButtonItem alloc]initWithCustomView:btn_map];
self.navigationItem.rightBarButtonItems=[[NSArray alloc]initWithObjects:list_bar,map_bar, nil];
Problem of tried code: There is a space between two buttons.
How can I achieve this?
Just use Segmented Control: you have a full example HERE
declare a segment control with custom view
UISegmentedControl *control = (UISegmentedControl *) [segmentBarButton customView];
and then add it to barbuttonitem view
UIBarButtonItem *segmentBarButton=[[UIBarButtonItem alloc] initWithCustomView:control];
dont forget to write the lines that relates to segment control customized view like ur requirement adding red or black images to the segment 0 or 1.

Make custom Navigation Buttons glow when touched?

In my navigation bar I have two right button bar items. One is a custom button and the other uses the info light button type. Both work great but I would like to make the custom button have the default glow the same way the info button does when you touch it. Is this possible?
// Default info light btn
UIButton *infoBtn = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIBarButtonItem *infoBtnItem = [[UIBarButtonItem alloc]initWithCustomView:infoBtn];
// Custom Btn
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setFrame:CGRectMake(0, 0, btnImage.size.width, btnImage.size.height)];
[customButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(rightDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customButtonItem = [[UIBarButtonItem alloc] initWithCustomView:wlButton];
NSArray *rightBtns = #[customButtonItem, infoBtnItem];
[navItem setRightBarButtonItems:rightBtns animated:YES];
you just need to put one more line of code like:-
customButton.showsTouchWhenHighlighted = TRUE;
After this your code Output something like this:-

MFMailComposeViewController customize

I am customizing MFMailComposeViewController it is working fine in ios 5.0 and ios 5.1 but not working properly in ios 6. The custom send and cancel button does not appear in mailcontroller.
My code is :
sendBtn = mailer.navigationBar.topItem.rightBarButtonItem;
cancelBtn = mailer.navigationBar.topItem.leftBarButtonItem;
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigation.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationItem *mailVCNavItem = [mailer.navigationBar.items objectAtIndex:0];
// Get the old bar button item to fetch the action and target.
UIBarButtonItem *oldCancelBarButton = [mailVCNavItem leftBarButtonItem];
// Create your new custom bar button item.
// In my case I have UIButton with image set as a custom view within a bar button item.
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:#"cancel-button-hover.png"] forState:UIControlStateNormal];
[backButton addTarget:oldCancelBarButton.target action:oldCancelBarButton.action forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0, 0, 55, 28)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
UIButton *sendbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[sendbtn setImage:[UIImage imageNamed:#"send-btnComment.png"] forState:UIControlStateNormal];
[sendbtn addTarget:self action:#selector(sendMail:) forControlEvents:UIControlEventTouchUpInside];
[sendbtn setFrame:CGRectMake(0, 0, 55, 28)];
self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:sendbtn];
MFMailComposeViewController and the related Facebook and Twitter sharing views are implemented through remote view controllers in iOS 6. This means that the controllers are run in another process and it is no longer possible to customize them by accessing their properties or subviews directly. You can still do so through UIAppearence though, but what you are trying to with replacing the buttons is no longer possible in iOS 6.

Create UIBarButtonItem with back button style

I'm looking for a way to create programmatically an UIBarButtonItem that looks like a back button of UINavigationBar.
Apparently seems like that the back button appears only after a push on the UINavigationController.
So I'm able to insert only a button with the "cancel" style. But my goal is to create a button with the "New Item" style.
Ideas ?
the short answer is you cannot do it.
you can save an image and you can put the image to that position, but the back button is managed by private part of the UINavigationBar.
I think you need to use image. and then set the image be the buttons backgroundImage. such as :
navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navigationItem = [[[UINavigationItem alloc] initWithTitle:#"Detail"] autorelease];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button setImage:[UIImage imageNamed:#"plain.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
initWithCustomView:button];
navigationItem.leftBarButtonItem = buttonItem;
[buttonItem release];
[button release];
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
Take a look on this answer : Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar
It gives you a psd file with image that you need.
It works for me:
[self.navigationItem.leftBarButtonItem setTitle:#"Back"];
(custom text with back arrow)

UIBarButtonItem with image and text, and a border?

I would like to create a UIBarButtonItem with an icon and text. I've seen this solution and this one but when you set the background image it doesn't draw a border.
I don't really want to draw a border (that looks like the "Plain" style one) with my icon on it. Is there another way around this?
If you look at the first solution you linked to you will notice the following line
UIBarButtonItem *barButton= [[[UIBarButtonItem alloc] initWithCustomView:chatButton] autorelease];
You can initialize a button with whatever view you would like. You can make a button with any (acceptable) view inside it.
eg.
// GRAPHICAL BUTTON FRAMEWORK
UIButton* btton = [UIButton buttonWithType:UIButtonTypeCustom];
[btton setFrame:CGRectMake(0, 0, 30, 30)];
[btton addTarget:self action:#selector(SOMEROUTINE) forControlEvents:UIControlEventTouchUpInside];
[btton setImage:[UIImage imageNamed:#"SOME IMAGE"] forState:UIControlStateNormal];
UIBarButtonItem* remix = [[[UIBarButtonItem alloc] initWithCustomView:btton] autorelease];
Hope that helps!

Resources