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
Related
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.
Is it possible to change the back button on a UINavigationBar and change the title?
When I try to set the customView property, I get an image right next to the default button.
I use this code
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:nil action:nil];
I want the title to be "Back" which is easy enough to do in Storyboard. But the problem is that no matter if I use code above or use customView property, the default back button remains.
You can add a UIImage to the UIButton. And, then use it as a custom back button. Here's a quick example:
// Custom image
UIImage *backButtonImage = [UIImage imageNamed:#"button-background-image.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setFrame:CGRectMake(0, 0, 100, 30)];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
// Custom title
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(barPayButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[backButton setShowsTouchWhenHighlighted:NO];
UIBarButtonItem *buttonOnBar =[[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = buttonOnBar;
Note: You will loose the chevron (system-provided back arrow) which was introduced in iOS7. It goes as a title and chevron together presenting your previous view controller.
Update:
You can also use UIEdgeInsets to resize your image intelligently.
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 0, 15, 10);
UIImage *backButtonImage = [[UIImage imageNamed:#"button-background-image.png"] resizableImageWithCapInsets:edgeInsets];
You can achieve what you want by setting the navigationItem.leftBarButtonItem of the view controller that's being pushed to the custom bar button item with the look you want. Good Luck!
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....
This is how I set button navigation bar
UIButton *addEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addEditButton setImage:[UIImage imageNamed:#"edit.png"] forState:UIControlStateNormal];
[addEditButton setFrame:CGRectMake(0, 0, 62, 31)]; used frame same as image
[addEditButton addTarget:self action:#selector(EditTable) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *addEdit = [[UIBarButtonItem alloc] initWithCustomView:addEditButton];
self.navigationItem.leftBarButtonItem =addEdit;
Everything works perfectly but button get pressed when I touch out side of it. How to solve this is there any way so it get pressed only if I touch on it
This is that image
apple has set this thing in way so user can navigate smoothly. it is advisable not to make such design in which you are putting buttons near navigationBarButton. there are some way to do it but its not good to change this kind of things. its just like reply to message and delete message both button are near beside to each other
I believe that you got this issue because the image you are setting is smaller thant the actual size of the button. please either make the button size smaller or provide a bigger image.
I hope this helps you.
It seems it works like that only, still I came up with a solution.
One is you can hide your navigation bar and use a toolbar indeed.
Other is you can add another button after that and set it's enabled property to FALSE.
I don't know whether this is proper or not, but seems to fulfill your requirement at least.
Here is the code:
UIButton *addEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addEditButton setFrame:CGRectMake(0, 0, 62, 31)];
[addEditButton setImage:[UIImage imageNamed:#"edit.png"] forState:UIControlStateNormal];
[addEditButton addTarget:self action:#selector(EditTable) forControlEvents:UIControlEventTouchUpInside];
UIButton *addEditButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[addEditButton1 setFrame:CGRectMake(63, 0, 30, 31)];
UIBarButtonItem *addEdit = [[UIBarButtonItem alloc] initWithCustomView:addEditButton];
UIBarButtonItem *addEdit1 = [[UIBarButtonItem alloc] initWithCustomView:addEditButton1];
addEdit1.enabled = FALSE;
NSMutableArray *buttonArray=[[NSMutableArray alloc]initWithCapacity:2];
[buttonArray addObject:addEdit];
[buttonArray addObject:addEdit1];
self.navigationItem.leftBarButtonItems =buttonArray;
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!