I'm creating custom UINavigationItem with custom background image and title. The image is shown but the title doesn't. What could be wrong?
I'm doing it this way:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"bar_button_black.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"bar_button_black.png"] forState:UIControlStateHighlighted];
[button setTitle:#"Cancel" forState:UIControlStateNormal];
[button setTitle:#"Cancel" forState:UIControlStateSelected];
[button setTitle:#"Cancel" forState:UIControlStateHighlighted];
[button addTarget:self action:#selector(closeButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 49, 30)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
You need to set image as backgroundImage not direct. If you set image like this
[button setImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
the title will not be displayed
set like this
[button setBackgroundImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
I think you must remove:
[button setImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
After that, the title will appear
Either remove this line
[button setImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
or you can change the Title color to some other color.i hope it will solve your problem.
make it to DrakgrayColor or some other color which can overcome your image color.
OK. Found a workaround, however I'm not sure whether it is allowed:
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Close",#"Close") style:UIBarButtonItemStylePlain target:self action:#selector(closeButtonClicked)];
[self.navigationItem setLeftBarButtonItem:closeButton];
for (UIView *view in self.navigationController.navigationBar.subviews) {
if ([[[view class] description] isEqualToString:#"UINavigationButton"]) {
UINavigationButton *button = {(UINavigationButton *)view};
[button setBackgroundImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"bar_button_black.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"bar_button_black.png"] forState:UIControlStateHighlighted];
}
}
Related
How I can set UIImage in the normal state and set title in the selected state in my UIButton?
Hi, I try to set a UIImage in the normal state of UIButton, but when set an UImage in normal state this change the title of UIButton in the selected state changing the title for the image . I am using
myButton setTitle:content forState:UIControlStateSelected
in the state selected.
How I can set UIImage in the normal state and set title in the selected state in my UIButton?
PDT: sorry for my english!.
Try This:
BOOL selected;
-(IBAction)btnClicked:(UIButton *)sender{
if(selected)
{
[chkBtn setTitle:#"selected" forState:UIControlStateNormal];
chkImage.image=[UIImage imageNamed:#" "];
NSLog(#"%#",chkBtn.titleLabel);
selected=NO;
}
else
{
[chkBtn setTitle:#" " forState:UIControlStateNormal];
chkImage.image=[UIImage imageNamed:#"button_minus_pink.png"];
selected=YES;
}
}
// set with UIColorĀ
[self.button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
// set with UIImage
[self.button setBackgroundImage:[UIImage imageNamed:#"BlackRightBtn.png"] forState:UIControlStateNormal];
For Button Selected
if (self.button.isSelected == YES)
{
[self.button setTitle:#"Your_ButtonTitle" forState:UIControlStateNormal];
}
add your code like this:
[myButton setTitle:#"Your String" forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton setTitle:#"" forState:UIControlStateNormal];
where img.png should not be there in your bundle
you can set this through Interface Builder as well.
You can define button and setImage for normal and selected state. Like this way.
buttonJobs = [UIButton buttonWithType:UIButtonTypeCustom ];
buttonJobs.tag = 1;
[buttonJobs setTitle:#"Jobs" forState:UIControlStateNormal];
buttonJobs.frame = CGRectMake(2, 56, 109, 33);
[buttonJobs setImage:[UIImage imageNamed:#"tabButtonImage_Off.png"] forState:UIControlStateNormal];//buttonJobs.png
[buttonJobs setImage:[UIImage imageNamed:#"tabButtonImage_On.png"] forState:UIControlStateDisabled];
[buttonJobs addTarget:self action:#selector(btnNextTab:) forControlEvents:UIControlEventTouchUpInside];
And in button selector method you can set your button selected according sender's tag.
-(void)btnNextTab:(UIButton*)sender
{
if(sender.tag == 1)
{
sender.selected = YES;
}
}
And for Title you can change color of text.
Hope this will help you.
Check with this code:
[myButton setTitle:content forState:UIControlStateSelected];
[myButton setImage:nil forState:UIControlStateSelected];
[myButton setImage:yourImage forState:UIControlStateNormal];
Try this code:
[myButton setTitle:#"Your String" forState:UIControlStateSelected];
[myButton setImage:nil forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton setTitle:#"" forState:UIControlStateNormal];
The UIButton is set as following code:
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 27, 27)];
[btn setBackgroundImage:[UIImage imageNamed:#"imgUp.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"imgDown.png"] forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
If I touch the btw quickly, the imgDown.png will not appear but the action btnPressed: is fired. How could it be fixed? Any help is appreciated:)
Add this line in your code:
[btn setBackgroundImage:[UIImage imageNamed:#"imgDown.png"] forState:UIControlStateSelected];
You should call the setHiglighted: method for the button (btn) inside of btnPressed: function.
The following code can solve the problem.
-(void)btnPressedDelay{
//original code in btnPressed: method
}
- (void) btnPressed:(id)sender
{
[self performSelector:#selector(btnPressedDelay) withObject:nil afterDelay:0];
// 0 did the magic here
}
its working fine i will tested it pls check your images and try it :
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 27, 27)];
[button setBackgroundImage:[UIImage imageNamed:#"rainy.jpg"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"Icon.png"] forState:UIControlStateHighlighted];
[button addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view button];
I am new to iPhone,
I want to implement radio button in my app, there are three buttons in my app, button should behave like radio button.
Here is my code snippet,
UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn1 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn1 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];
UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn2 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn2 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];
UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn3 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn3 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];
- (IBAction)RadioButton:(UIButton *)button{
for (UIButton *Radiobutton in [self.view subviews]) {
if ([Radiobutton isKindOfClass:[UIButton class]] && ![Radiobutton isEqual:button]) {
[Radiobutton setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
}
Any help will be appreciated.
You can use following code.
//Add all the buttons to class level NSMutable array
UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn1 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];
[self.buttonsArray addObject:Btn1];
UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn2 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];
[self.buttonsArray addObject:Btn2];
UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn3 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];
[self.buttonsArray addObject:Btn3];
- (IBAction)RadioButton:(id)sender{
[self resetAllButtons];
UIButton* button=(UIButton*) sender;
[button setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateNormal];
}
-(void) resetAllButtons{
for(int i=0;i<[self.buttonsArray count];i++){
UIButton* button=[self.buttonsArray objectAtIndex:i];
[button setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
}
}
You are checking all views in self.view.subviews but the buttons are actually added to the UIScrollView (I guess) called scrollVw.
Change the for loop to
for (UIButton *Radiobutton in [self.scrollVw subviews]) {
if the scrollVw is added as a property in the .h file.
Any opposition to using a UISegmentedControl? It works like a radio button, in that it can only have one option selected at a time. If you are deadset on the appearance of radio buttons I would subclass it to change the appearance.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UISegmentedControl_Class/Reference/UISegmentedControl.html
Alternatively you could simply use the iOS provided Control for something like that:
UISegmentedControl
It can easily handle three segments, is quite flexible and can behave like a radio button where you can select only one segment or multiple.
So I am trying to create a custom UIBarButtonItem, but I can't for the life of me figure out why the title won't show. Maybe someone else can spot it :\
Here is the code I use to create the bar button:
+ (UIBarButtonItem *)barButtonItemWithTitle:(NSString *)title normal:(UIImage *)normal highlighted:(UIImage *)highlight selected:(UIImage *)selected target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0.0f, 0.0f, normal.size.width, normal.size.height)];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setImage:normal forState:UIControlStateNormal];
[button setImage:selected forState:UIControlStateHighlighted];
[button setImage:selected forState:UIControlStateSelected];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:#"Cancel"
normal:[UIImage imageNamed:#"back_button_active.png"]
highlighted:[UIImage imageNamed:#"back_button_highlight.png"]
selected:[UIImage imageNamed:#"back_button_selected.png"]
target:self
action:#selector(popViewController)];
Any help would be appreciated!
P.S. This is in iOS 5 using ARC.
title and image are mutually exclusive properties in UIButton. If you want to have title with custom background then you should use backgroundImage property:
...
[button setBackgroundImage:normal forState:UIControlStateNormal];
[button setBackgroundImage:selected forState:UIControlStateHighlighted];
[button setBackgroundImage:selected forState:UIControlStateSelected];
...
I'm customizing the UIButton programmatically here:
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setSelected:YES];
button.frame = CGRectMake(x, y, width, height);
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitle:#"Button Title" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateDisabled];
The issue is if I'm holding down the image background image disappears until I'm releasing it...
I think you're in overkill mode :). Try setting button.png for UIControlStateNormal and buttonActive.png for UIControlStateHighlighted. No need for the rest. See if this works.
EDIT:
Also, remember: Image file names are case sensitive
Are you testing on device? Image names are case sensitive for device builds, but not for the simulator. For example, if your actual image file is named buttonactive.png, but you're calling it as buttonActive.png from your code, it will show up on the simulator, but not on the device.
Please ensure that the case for both image names matches the name of the actual file.
EDIT #2:
Try this code
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setSelected:YES];
button.frame = CGRectMake(x, y, width, height);
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitle:#"Button Title" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateHighlighted];
Figure it out, it works in this way:
[_whateverButtonTab setBackgroundImage:[UIImage imageNamed:#"ActivateButton.png"] forState:UIControlStateSelected];
[_whateverButtonTab setBackgroundImage:[UIImage imageNamed:#"ActivateButton.png"] forState:(UIControlStateHighlighted|UIControlStateSelected)];
While we add a button from IDE
sample:
.h file
-(IBAction)BtnAdd:(id)sender
in .m file it is
-(IBAction)BtnAdd:(id)sender
{
}
It is a method that cannot enable or disable.
so if you want to enable or disable button make it as -(IBOutlet)BtnAdd
add IBOutlet to .h file And connect it to the particular button
then BtnAdd.enabled=NO will work