Navigation bar with UIImage Button and subtitle - ios

I want to recreate something like the below image which is navigation bar with UIImage buttons and subtitle.
My current code looks something like in below
UIImage * cropModeImage = [UIImage imageNamed:#"crop"];
UIButton *cropModeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cropModeButton setImage:[cropModeImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
[cropModeButton addTarget:self action:#selector(_setCropMode:) forControlEvents:UIControlEventTouchUpInside];
UILabel *subtitle = [[UILabel alloc]initWithFrame:CGRectMake(8, 20, 185, 30)];
[subtitle setBackgroundColor:[UIColor clearColor]];
[subtitle setFont:[UIFont fontWithName:#"Helvetica" size:14]];
subtitle.text=#"This is the sub";
[subtitle setTextColor:[UIColor blackColor]];
[cropModeButton addSubview:subtitle];
[self.view addSubview:cropModeButton];
UIBarButtonItem *cropMode = [[UIBarButtonItem alloc] initWithCustomView:cropModeButton];
Can anyone help me with it?

In iOS, this is done using tabbar controller which generally appears at the bottom of the screen and if you want to show on the top of your screen then use the toolbar.
Look at the calendar app of iPhone, it has 3 buttons to the right side and that's how it will look if you add buttons in the navigation bar.
I would prefer using the storyboard OR XIB for this and add all the barbutton items there and assign IBAction methods to it, here's an example of how you can use storyboard for the same.
The buttons used in the toolbar can have images with captions to display the same look and feel.
Hope it helps.

Related

Can't See Custom uibarbuttonitem at Right side of navigation bar

I am trying to add custom navigation bar button at right side of navigation bar.
here is my code.
UIButton *customPrintButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customPrintButton setFrame:CGRectMake(0, 0, 100, 30)];
[customPrintButton.titleLabel setTextColor:[appDelegate getAppPinkColor]];
[customPrintButton setTitle:#"Print" forState:UIControlStateNormal];
[customPrintButton setBackgroundColor:[UIColor clearColor]];
[customPrintButton setExclusiveTouch:YES];
[customPrintButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
[customPrintButton.titleLabel setFont:[UIFont systemFontOfSize:20]];
[customPrintButton addTarget:self action:#selector(printOutButton_Clicked) forControlEvents:UIControlEventTouchUpInside];
printOutBtn = [[UIBarButtonItem alloc]initWithCustomView:customPrintButton];
self.navigationItem.rightBarButtonItem = printOutBtn;
but still i can't see the button on my page..
I forgot to add title color..
that's why default it takes white color as text color
Thanks for reply.

UIButton has dot when set to selected inside UIToolbar

I'm adding a UIButton to a UIToolbar that I don't want to have as a UIBarButtonItem so that I have more control over the position of the UIButton inside the frame. I set a different title for both "Normal" and "Selected" states. Everything acts completely normal for a UIButton except when it's "Selected" there is a 1 pixel dot in the front of the UIButton's title.
toolbar:
UIToolbar *customView = [[UIToolbar alloc] initWithFrame:CGRectMake(HEADER_MARGIN, 0, HEADER_WIDTH, HEADER_HEIGHT)];
[customView setBarTintColor:[UIColor whiteColor]];
button:
UIButton *listOrderButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ORDER_BUTTON_WIDTH, ORDER_BUTTON_HEIGHT)];
[listOrderButton setCenter:CGPointMake(customView.center.x, HEADER_HEIGHT/2)];
[listOrderButton.titleLabel setFont:[UIFont systemFontOfSize:ORDER_BUTTON_FONT_SIZE]];
[listOrderButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
[listOrderButton setTitle:#"ABC" forState:UIControlStateNormal];
[listOrderButton setTitle:#"Time" forState:UIControlStateSelected];
[listOrderButton setTitleColor:[UIColor blueAppColor] forState:UIControlStateNormal];
[listOrderButton addTarget:self action:#selector(listOrderChange:) forControlEvents:UIControlEventTouchUpInside];
[customToolbar addSubview:listOrderButton];
and the selector:
- (void)listOrderChange:(UIButton *)sender {
[sender setSelected:!sender.selected];
}
Image of what's happening:
- No dot when not selected
- When selected, there is a dot
This happens both on the device and in the simulator. Changing the text color for any and all states has no bearing on the color of the dot itself.
I should also point out that the dot is two points wide and one point high.
Instead of using UIButton, I know you dont want to use a UIBarButton, but would it be possible to create a UIBarButton programmatically giving it all the features of a UIButton?
Since UITabBar is like UINavigation Bar and apple suggests using UIBarButtonItem for bar/tabBar Buttons... I believe using a UIBarButtonItem might solve the problem....

iOS Custom Font "Floats" Too High

I'm creating a custom UIBarButtonItem, like so:
UIImage *originalImage = [UIImage imageNamed:#"button"];
UIImage *buttonImage = [originalImage stretchableImageWithLeftCapWidth:10 topCapHeight:5];
UIButton *toolbarB = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 65, 29)];
[toolbarB setTitle:title forState:UIControlStateNormal];
[toolbarB.titleLabel setFont:[UIFont fontWithName:kLatoBold size:17.0f]];
[toolbarB setBackgroundImage:buttonImage forState:UIControlStateNormal];
[toolbarB addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarB];
Depending on which of my custom fonts I use, after inserting my UIBarButtonItem into my controller's navigationItem, I get one of the results below.
Why does the button text in the second result "float" higher than it is supposed to? The first button looks great, but the second one positions the text in an unnatural fashion... Could it be a problem with my font?
Button Text Displaying Properly
Annoying, Floating Button Text that is Too High!
It happens for some fonts, I don't know why, but I can suggest you to use UIEdgeInsets to push your text down a bit. I guess you cannot fix your font, so;
Here is a link for a similar solution; Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

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