I need a toolbar button with small text under button icon.
I'm trying on viewDidLoad to do:
UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setImage:[UIImage imageNamed:#"menu-search-active.png"] forState:UIControlStateNormal];
[customButton setTitle:#"Button" forState:UIControlStateNormal];
[customButton sizeToFit];
UIBarButtonItem* sb = [[UIBarButtonItem alloc] initWithCustomView:customButton];
[self setToolbarItems:[NSArray arrayWithObject:sb]];
But my button have only icon, no title visible.
Related
I want to get a button with short lines on UINavigationBar.
Please, see that on this image:
What is it? How to get it?
You'll want to instantiate a UIBarButtonItem with a custom view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 24, 24);
[button setImage:[UIImage imageNamed:#"your_image_filename"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTappedMethod) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
This creates a UIButton with a supplied image. That UIButton is then set as a custom view for a UIBarButtonItem. Then, just set the barButtonItem on your navigation item.
I'm trying to create a back bar button programmatically, similar to the iOS 7 default back button. But when I add my back bar button to the navigation item. The Chevron plus is missing. How can I achieve this?
UIImage* image_back = [UIImage imageNamed:#"leftarrow.png"];
CGRect backframe = CGRectMake(15, 5, 15,21);
UIButton *backbutton = [[UIButton alloc] initWithFrame:backframe];
[backbutton setBackgroundImage:image_back forState:UIControlStateNormal];
[backbutton addTarget:self action:#selector(Btn_back:)
forControlEvents:UIControlEventTouchUpInside];
[backbutton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *backbarbutton =[[UIBarButtonItem alloc] initWithCustomView:backbutton];
self.navigationItem.leftBarButtonItem=backbarbutton;
[backbutton release];
-(IBAction)Btn_back:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
I am creating a custom UINavigationBar BarButtonItem as follows:
// Set up bar button items
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35.0f, 35.0f)];
[backButton setImage:[UIImage imageNamed:#"myimg.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(myselector) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
This is working perfectly, but when I go to set the title of the UIButton or BarButtonItem, it won't show up. Any suggestions?
Tried...
[backButton setTitle:#"Edit" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
And...
self.navigationItem.leftBarButtonItem.title = #"Edit";
without luck.
The problem is that image and title use same viewspace for display.
So when we use image with title first it show image and than title (right side of image).
If size of UIButton is same as image than we can't see Title.
A simple solution is set image in background. So try this:
[backButton setBackgroundImage:[UIImage imageNamed:#"myimg.png"] forState:UIControlStateNormal];
UIImage *image = [UIImage imageNamed:#"myimg.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:image forState:UIControlStateNormal];
[backButton setTitle:#"Edit" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]
backButton.frame=CGRectMake(0, 0, 35.0f, 35.0f);
UIBarButtonItem *barButton= [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
self.toolbar.items = [NSArray arrayWithObject:barButton];
I'm trying to add an icon (and keep the title) to the back button of my navigation controller. It seems if I set the image of the UIBarButtonItem it hides the title, so I thought I'd try a custom view. I've tried
UIButton* customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setImage:[UIImage imageNamed:#"icon"] forState:UIControlStateNormal];
[customButton setTitle:#"Title" forState:UIControlStateNormal];
[customButton setAdjustsImageWhenHighlighted:YES];
[customButton setFrame:CGRectMake(0, 0, 125, 32)];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.backBarButtonItem = backButton;
but Apple docs say that the backBarButtonItem ignores custom views, so this doesn't work.
I also tried this:
self.navigationItem.leftBarButtonItem = backButton;
self.navigationItem.hidesBackButton = YES;
but the leftBarButtonItem shows up one screen too soon and the hidesBackButton doesn't seem to hide the backBarButtonItem.
Is there another way to get both an image and title onto a navigation backBarButtonItem?
Very first hide the back button provided by UINavigationController by susing the code self.navigationItem.hidesBackButton = YES;
And set own button with image and title with the following code
UIButton* customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setImage:[UIImage imageNamed:#"icon"] forState:UIControlStateNormal];
[customButton setTitle:#"Title" forState:UIControlStateNormal];
[customButton setAdjustsImageWhenHighlighted:YES];
[customButton setFrame:CGRectMake(0, 0, 125, 32)];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.backBarButtonItem = backButton;
I am trying to add image to UIBarButtonItem in IOS App. I am able to add it but the problem is image is taking the size of the button. But I want button to be of the size of the image with borders included in it.
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setBackgroundImage:[UIImage imageNamed:#"customButtonBgImage.png"] forState:UIControlStateNormal];
[customButton setImage:[UIImage imageNamed:#"customButtonImage.png"] forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
And the you can add barButtonItem where-ever you need it.