I am using the following code for a custom back button with title in the nav bar. However, the title is not being shown, any help ?
nb: the back button image is black and the same one thats used by default by ios, needed to customize the action handler, thats why did it this way.
backButton=[UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:#"backbutton.png"] forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:#"backbutton.png"] forState:UIControlStateSelected];
[backButton addTarget:self action:#selector(customBack) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0,0,64,32)];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[backButton setTitle:#"Encameo" forState:UIControlStateNormal];
[backButton setTitle:#"Encameo" forState:UIControlStateSelected];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView: backButton];
You are setting image on button instead of this you need to set backgroundImage of UIButton then your title will be displayed.
Change the code like this
backButton=[UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:[UIImage imageNamed:#"backbutton.png"] forState:UIControlStateNormal];
[backButton setBackgroundImage:[UIImage imageNamed:#"backbutton.png"] forState:UIControlStateSelected];
[backButton addTarget:self action:#selector(customBack) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0,0,64,32)];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[backButton setTitle:#"Encameo" forState:UIControlStateNormal];
[backButton setTitle:#"Encameo" forState:UIControlStateSelected];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView: backButton];
Related
I want to show image and the title on RightBar Button so i have written code as below:
UIBarButtonItem *negativeSpacer = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
negativeSpacer.width = BARBUTTONITEM_PADDING_IPAD;
UIImage *buttonImage = [UIImage imageNamed:#"select-ipad#3x.png"];
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectButton setImage:buttonImage forState:UIControlStateNormal];
[selectButton setTitle:#"Select" forState:UIControlStateNormal];
[selectButton setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
[selectButton.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:14.0]];
[selectButton sizeToFit];
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:selectButton];
[selectButton addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,aBarButtonItem,nil]];
This code shows the title and image on the NavigationBar and when i click on it, then the clicked like appears only on the image but not on the text.
-(void)viewWillAppear:(BOOL)animated{
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[btn setImage:[UIImage imageNamed:#"select-ipad#3x.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(onBtnRightClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnRight=[[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = btnRight;
}
-(IBAction)onBtnRightClick:(id)sender
{
NSLog(#"Right Bar Button Clicked.");
}
Using your code, try 2 buttons:
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(0.0, 0.0, 45.0, 45.0)];
[button1 addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:#"select-ipad#3x.png"] forState:UIControlStateNormal];
UIBarButtonItem *buttonItem1 = [[UIBarButtonItem alloc]initWithCustomView:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:#"Select" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button2.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:14.0]];
[button2 sizeToFit];
UIBarButtonItem *buttonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
self.navigationItem.rightBarButtonItems = #[buttonItem2,buttonItem1];
I hope this can help you.
Below is the code what i was trying but didn't get , please any knows post the answer thanks in advance
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeSystem];
doneButton.frame = CGRectMake(0.0, 0.0, 48.0, 32.0);
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
[doneButton setTitleColor:[UIColor colorWithRed:37/255.0 green:180/255.0 blue:185/255.0 alpha:1.0] forState:UIControlStateNormal];
[doneButton setTitleColor:[UIColor colorWithRed:76.0/255.0 green:76.0/255.0 blue:76.0/255.0 alpha:1.0] forState:UIControlStateHighlighted|UIControlStateSelected];
[doneButton.titleLabel setFont:[CSUtilities latoRegularFontForSize:18.0]];
[doneButton setBackgroundColor:[UIColor clearColor]];
[doneButton addTarget:self action:#selector(doneAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
self.navigationItem.rightBarButtonItem = doneButtonItem;
On the doneAction Make Button Selected
-(void)doneAction:(UIButton *)sender{
sender.selected=true;
}
You dont need to add the UIControlStateSelected in your code. Just use UIControlStateHighlighted alone.
[doneButton setTitleColor:[UIColor colorWithRed:76.0/255.0 green:76.0/255.0 blue:76.0/255.0 alpha:1.0] forState:UIControlStateHighlighted];
i've programmatically created a button with an image for normalState.
But I want to set a new button image when the button is pressed (the new image should be displayed for the rest of the time). I tried something, but it only works during tapping. So the button shows its old image after tapping again.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
[cell addSubview:button];
Thanks for your help.
Try to use this it will helps you.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"image2png"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
// ----------- Add this line in your code -------
[button addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
- (void)btnPressed:(UIButton *)sender
{
[sender setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateNormal];
}
You have to do the setting in the target of the button. Add the following code:
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
and set the image in that method for UIControlStateNormal:
- (void)buttonTapped:(UIButton *)sender {
[button setBackgroundImage:[UIImage imageNamed:#"image2png"] forState:UIControlStateNormal];
}
I use the following code to load an image is on the upper, lower half is the title of the button, but only the image, is this why? Have any good Suggestions please let us know, thank you
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(160, 400, 73, 44)];
[myButton setImage:[UIImage imageNamed:#"icon.png"] forState:UIControlStateNormal];
[myButton setImageEdgeInsets: UIEdgeInsetsMake(0,0,22,0)];
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton setTitleEdgeInsets: UIEdgeInsetsMake(22,10,0,10)];
[self.view addSubview:myButton];
you can achieve this effect by this way also.. Try this ::
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(160, 400, 73, 55)];
UIImage *img = [UIImage imageNamed:#"icon.png"];
UIImageView *imgVw = [[UIImageView alloc ] init];
imgVw.image = img;
imgVw.frame = CGRectMake(myButton.frame.origin.x, myButton.frame.origin.y, myButton.frame.size.width, (myButton.frame.size.height-18));
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton setTitleEdgeInsets: UIEdgeInsetsMake(34,10,0,10)];
[myButton addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:imgVw];
[self.view addSubview:myButton];
Your button method
-(void) btnClick:(id)sender
{
NSLog(#"working");
}
You can achieve from your logic also :: Here i've edited some code in your logic
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(160, 400, 40, 35)];
[myButton setImage:[UIImage imageNamed:#"icon.png"] forState:UIControlStateNormal];
[myButton setImageEdgeInsets:UIEdgeInsetsMake(0,(myButton.frame.size.width-52),17,0)];
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton setTitleEdgeInsets: UIEdgeInsetsMake(22,(myButton.frame.size.width-78),0,22)];
[self.view addSubview:myButton];
but, in this logic you have to resize your image to 23*23 pixels. according to your image size UIEdgeInsetsMake's parameter will be changed.
Hope this helps. Happy coding.
I'm making a custom UIButton with 2 images, but for some reason the title is just not showing up, the button is showing fine, but without any title.
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(10, 250, 300, 43);
[_button addTarget:self action:#selector(loginClicked:) forControlEvents:UIControlEventTouchUpInside];
[_button setImage:[UIImage imageWithContentsOfFile:normalButtonImg] forState:UIControlStateNormal];
[_button setImage:[UIImage imageWithContentsOfFile:hoverButtonImg] forState:UIControlStateSelected];
[_button setTitle:#"Login" forState:UIControlStateNormal];
[_button setTitle:#"Login" forState:UIControlStateHighlighted];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self.view addSubview:_button];
These two lines:
[_button setImage:[UIImage imageWithContentsOfFile:normalButtonImg] forState:UIControlStateNormal];
[_button setImage:[UIImage imageWithContentsOfFile:hoverButtonImg] forState:UIControlStateSelected];
should be
[_button setBackgroundImage:[UIImage imageWithContentsOfFile:normalButtonImg] forState:UIControlStateNormal];
[_button setBackgroundImage:[UIImage imageWithContentsOfFile:hoverButtonImg] forState:UIControlStateSelected];
Explanation: if a foreground image is set for UIButton, the image is placed above the title, so it might hide it. If you want an image and a title to be displayed, you have to set a background image.
When you set the image, that overrides the title. If you can make your image a background image, that will work. Otherwise you will need to create a composite image for the button by essentially creating a new image which is your desired image with the title drawn over it.