I need a quick way to change the color of an UIButton live (in response to a delegate call) . So the button is drawn with a white background (standard) then when a delegate call comes it needs to change to a blue/red background.
I tried :
btn.backgroundColor = [UIColor redColor];
[btn setNeedsDisplay];
This doesn't work...
Also it would be a good ideea to change the color of the button text... How is this done????
Try using it like this :
[button setBackgroundColor:someColor forState:UIControlStateNormal];
Did it work for you .. ? I am assuming you have made your button a custom button type.
If it worked, you may want to consider, accepting the answer
First make your UIButton type Custom..
UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setbackgroundColor:[UIColor blueColor] forState: UIControlStateNormal];
[myButton setTitleColor:[UIColor blueColor] forState: UIControlStateNormal];
Related
I create a button like this, but when I click on it there's no changing effect.
UIButton *button = [[UIButton alloc] init];
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setTitle: #"xxxxx" forState: UIControlStateNormal];
[button setTitleColor: [UIColor colorWithRed:0.98 green:0.44 blue:0.05 alpha:1] forState: UIControlStateNormal];
I'm not going to use two images, I just want the text to change a bit. Just like a fresh button dragged to the UIViewController.
What did I miss?
You have to set a title and a title color when the button is clicked.
[button setTitle: #"clicked" forState: UIControlStateHighlighted];
[button setTitleColor: [UIColor redColor] forState: UIControlStateHighlighted];
See UIControl Class Reference
I have a hyperlink label in my application.Clicking on it opens the application i want.
The problem is after i click on it the font size of the label shrinks automatically.
Is there a way to avoid this??
Please suggest a solution for this issue. Thanks in advance
I would suggest you to use UIButton with UIButtonTypeCustom and clear background instead of UILabel. It will look like UILabel and in the action method you can perform any task.
UIButton *btn = [[UIButton alloc] initWithFrame:btn_frame];
[btn setBackgroundColor:[UIColor clearColor]];
[btn addTarget:self action:#selector(DoSomething) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle: #"Button_Title" forState: UIControlStateNormal];
[btn setTitleColor: #your_choice forState:UIControlStateNormal];
[btn setTitleColor: #your_choice forState:UIControlStateHighlighted];
btn.titleLabel.font = #your_choice
btn.titleLabel.textAlignment = #your_choice;
btn.contentVerticalAlignment = #your_choice;
if still you want UILabel here is link might help you..
Attributed Label
I want my UIButton to show up the highlighted state when I click over a button that is already selected.
Basically in the highlighted state I apply a *.png image as my UIButton backgroundImage to give a pressed down effect.
But if the button is already in the Selected State When I click over it again I just can't see the highlighted state but it goes straight to the normal state!
Watch the Issue--> Video of the Issue!
Help please
//0 init UIButton
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, aSide, aSide)];
//1 Give it a backgroundColor
[button setBackgroundColor:aColor];
[..]
//2 Set titleLabel and its style
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
UIImage *shadowImage = [UIImage imageNamed:kBtnShadow];
shadowImage = [shadowImage stretchableImageWithLeftCapWidth:floorf(shadowImage.size.width/2) topCapHeight:floorf(shadowImage.size.height/2)];
[button setBackgroundImage:shadowImage forState: UIControlStateHighlighted];
[button setTitle:aLabel forState: UIControlStateNormal];
//3 Assign tag and Action
[button setTag:tag];
[button addTarget:target action:a forControlEvents:UIControlEventTouchUpInside];
The various states: UIControlStateNormal, UIControlStateSelected, and (UIControlStateSelected | UIControlStateHighlighted) are all actually distinct. If you want your shadowImage to apply both in the (only) highlighted state and in the highlighted+selected state, you must also set:
[button setBackgroundImage:shadowImage forState:(UIControlStateHighlighted | UIControlStateSelected)]
In swift this would be:
button.setBackgroundImage(shadowImage, forState: UIControlState.Selected.union(UIControlState.Highlighted))
In Swift v3 (Nov. 2016):
button.setBackgroundImage(shadowImage, for: UIControlState.selected.union(UIControlState.highlighted))
Swift 4.2
Applicable programmatically only.
aButton.setImage(UIImage(named: "your_image"), for: [.selected, .highlighted])
I am using xcode 4.6 to develop an app. Here i want to add UIButton programmatically to UIscrollview. This is the code i follow.
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Now the problem is that Button gets disappeared (technically its textcolor becomes white) on click. I checked keeping UIscrollview color to red that th button was still in the view but i cant get the reason why its text color changed and how do i undo dis.
Basically I wan to create a clickable link using UIbutton.
I know uitextview approach (datadetectortype) but its of no use as i want to show different text in the label for the link and the actual link.
Note: The textcolor doesnt change back to blue and remains white only.
Thanks in advance.
Try the below code
UIButton *bt =[UIButton buttonWithType:UIButtonTypeCustom];
bt.frame = CGRectMake(50.0, 50.0, 100.0, 50.0);
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.scrollTest addSubview:bt];
-(void)userTappedOnLink:(UIButton*)sender
{
NSLog(#"Test ..");
[self performSelector:#selector(changeBtnTextColor:) withObject:sender afterDelay:1.0];
}
-(void)changeBtnTextColor:(UIButton*)btn
{
btn.titleLabel.textColor=[UIColor blueColor];
}
hope it will work for you.
use below code you will get your solution
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
[bt setBackgroundColor:[UIColor grayColor]];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Use the below code:
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Problem is you are set Title to titleLabel and you are changing textColor of buttonTitle color.
All the best !!!
check Button's Fram .. is it valid or not ??
and remove bt=[UIButton buttonWithType:UIButtonTypeCustom]; line from your code.
My build target is set for IOS5 which is where I understand UIButton.tintColor was introduced...
I have this in my viewDidLoad for the View Controller
[timelineButton setTintColor:[UIColor blackColor]];
[timelineButton setTitle:#"test" forState:UIControlStateNormal];
The text changes properly but the button isn't black?
Thanks!
According to the documentation:
This property is not valid for all button types.
You need to switch your buttonType to another one of these. (Unfortunately, the documentation does not specify which specific button types support this property.)
Make sure your button "type" is set to System. If it is set to Custom it could be the reason the color of your button isn't changing. This is what happened to me and changing the type to System fixed it.
It tint your highlighted State color.
When you tap/click on the UIButton the color specified with tintColor appears as long as you hold the tap/click on the UIButton.
resetButton.tintColor = [UIColor colorWithRed:0.764 green:1.000 blue:0.000 alpha:1.000];
The button is white in normal state.
But if I tap on the button the color turns red, but only then.
IF you need to change the button so it looks like a red or blue one in the UIControlStateNormal then
Change the UIButtonType to UIButtonTypeCustom in Interface Builder or programmatically with
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
Change the attributes on your own and recreate the rounded corners
resetButton.backgroundColor = [UIColor redColor];
resetButton.layer.borderColor = [UIColor blackColor].CGColor;
resetButton.layer.borderWidth = 0.5f;
resetButton.layer.cornerRadius = 10.0f;
As stated in other answers tint does not work for Custom Button types. Make sure you explicitly declare the button type. Do not just use [UIButton alloc] init]
This will work:
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mybutton setImage:[UIImage imageNamed:#"myImage"] forState:UIControlStateNormal];
mybutton.tintColor = [ODTheme blueTintColor];
I found 3 things must be followed.
1. Render image as Default
Go to Images.xcassets > Your Image > Show the Attributes inspector > Render As > Default
2. Make sure your button type is System
3. Now change button's tintColor.
Done.
Today, I also meet this problem. I use delegate to solve it.
[button addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:#selector(buttonPressReset:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
-(void)buttonPress:(id)sender{
UIButton* button = (UIButton*)sender;
[button setBackgroundColor:[UIColor greenColor]];
NSLog(#"buttonPressed");
}
-(void)buttonPressReset:(id)sender{
UIButton* button = (UIButton*)sender;
[button setBackgroundColor:[UIColor redColor]];
NSLog(#"buttonPressReset");
}
Should try this method:
- (void)setTitleColor:(UIColor *)color
forState:(UIControlState)state
In iOS 13 you can tint the image and then set it to the button:
let coloredImage = UIImage().withTintColor(UIColor.red)
UIButton().setImage(coloredImage, for: .normal)
Simply set your UIButton to type System in Storyboard.
And then in code just use:
myButton.tintColor = UIColor.whiteColor()
If your UIButton type is system then and then only tint colour property is work. So first you need to set your button type to the system then apply tint colour for the specific state.
let btn = UIButton.init(type: .system)
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
To change the color of the Button text you can use:
resetButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
Or OBJ-C:
- (void)setTitleColor:(UIColor *)color
forState:(UIControlState)state
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instm/UIButton/setTitleColor:forState:
Simply use:
[yourButton setBackgroundImage:[yourButton.currentBackgroundImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
[yourButton setTintColor:[UIColor blackColor]];
Swift 4 :
yourButton.setTitleColor(UIColor.white, for: .normal)
I couldn't really change the button type to System as I was inheriting from a closed internal library.
So, I changed the appearance API for UIButton
UIView.appearance(whenContainedInInstancesOf: [UIButton.self]).tintColor = .white