Override Apple's UIButton Highlight - iphone

I have a button, where I set its image and its selected & highlighted state image as follows:
UIImage *buttonImage = [UIImage imageNamed:#"btn_learn_pad"];
self.learnMoreButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 0, buttonImage.size.width, buttonImage.size.height)];
[self.learnMoreButton setImage:buttonImage forState:UIControlStateNormal];
[self.learnMoreButton setImage:[UIImage imageNamed:#"btn_learn_focus_pad"] forState:UIControlEventTouchUpInside | UIControlStateHighlighted | UIControlStateSelected];
[self.learnMoreButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
This still overlays Apple's darker alpha on top of the image upon selection. How do I remove this effect?
Thanks

In stead of setImage:forState use setBackgroundImage:forState:
[self.learnMoreButton setBackgroundImage:[UIImage imageNamed:#"btn_learn_pad"] forState:UIControlStateNormal];
[self.learnMoreButton setBackgroundImage:[UIImage imageNamed:#"btn_learn_focus_pad"] forState:UIControlStateHighlighted];

Related

Setting The Text On A Custom Button

I made a custom button with a custom image. Then I tried to set the title of it, but it didn't work. Here is the button's code
- (void)viewDidLoad
{
UIImage *backButtonImage = [UIImage imageNamed:#"button.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor colorWithRed:0.49 green:0 blue:0 alpha:1] forState:UIControlStateNormal];
[backButton setTitle:#"DONE" forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self action:#selector(goBackOne) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.hidesBackButton = YES;//hide original back button
}
But it still looks like this
You should use the backgroundImage property if you want the image to appear underneath the text:
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
If you use the image property, the image will push the text out of the way (it works when you want to show a small image and text side by side)
Try backButton.text = #"DONE".
Edit: Try changing this line:
[backButton setTitle:#"DONE" forState:UIControlStateNormal];
to
[backButton setText:#"DONE" forState:UIControlStateNormal];

What's the right UIControlState for the UIButton being pressed?

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

How to set the buttons label text color for state UIControlStateHighlighted

I am creating an iPhone application in which i have a custom button. i have set the buttons title by creating a Label and adding it as subview. now when the button is highlighted i want to change the labels text color.
here is my code,
UIButton *button1= [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(68,162, 635, 101)];
[button1 setImage:[UIImage imageNamed:#"startwithouttext.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"startactivewithouttext.png"] forState:UIControlStateHighlighted];
UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(button1.bounds.origin.x+50, button1.bounds.origin.y+20, button1.bounds.size.width-100, button1.bounds.size.height-40)];
[buttonLabel setFont:[UIFont fontWithName:#"Helvetica" size:28]];
buttonLabel.backgroundColor=[UIColor clearColor];
buttonLabel.textColor=[UIColor colorWithRed:83.0/255.0 green:83.0/255.0 blue:83.0/255.0 alpha:1.0];
buttonLabel.highlightedTextColor=[UIColor whiteColor];
buttonLabel.text = #"Long text string";
[button1 addSubview:buttonLabel];
[button1 bringSubviewToFront:buttonLabel];
[button1 setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[button1 setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[button1 addTarget:self action:#selector(button1clicked:) forControlEvents:
[mainView button1];
can any body help me to change the text color when the button is highlighted?
Found the answer in a different question on StackOverflow:
UIButton color issues
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
This is if you can work without creating a Label and adding it as subview as you mention above.
you can add target for UIControlStateHighlighted state of UIButton like
[button1 addTarget:self action:#selector(buttonHighlighted:) forControlEvents:UIControlStateHighlighted];
and in buttonHighlighted method you can change the color of your label's text
- (void) buttonHighlighted:(id)sender
{
//code here
}
Hope it gives you an idea.
For SelectedColor
[yourButton setTitleColor:[UIColor purpleColor] forState:UIControlStateSelected];
For HighlightedColor
[yourButton setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];

Title on custom UINavigationItem is not shown

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];
}
}

Setting border around custom UIBarButtonItem

I want to put a custom button as right navigation bar button item. Here is the code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"my.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(filterResult) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 50, 50)];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
The problem is that I can see only the image and not the button around it like it comes for UIBarButtonSystemItemAdd or other types.
What should I do?
[button setImage:[UIImage imageNamed:#"my.png"] forState:UIControlStateNormal];
replaces the entire image for the specified state. You have to draw the full own image and not only the icon on it (or whatever you want).