Change pressed color of UIButton [duplicate] - ios

This question already has answers here:
Is it possible to change a UIButtons background color?
(17 answers)
UIButton selection color [duplicate]
(5 answers)
Closed 9 years ago.
Is there any way to change the color of a UIButton when it's pressed?
The default seems to be a blue color, but I don't see where or how to change this to a custom color.
Thanks!

If you are using storyboard then in inspector window you can change the highlight tint property to the color you want on button click event.
Look at the Highlight Tint property in image.

I ultimately followed the following poster's suggestion. It worked perfectly.
https://stackoverflow.com/a/10670141/720175
In my particular case I created a subclass of UIButton, with the final code as:
-(void) setHighlighted:(BOOL)highlighted
{
if(highlighted) {
self.backgroundColor = [UIColor colorWithRed:1 green:0.643 blue:0.282 alpha:1];
} else {
self.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
}
[super setHighlighted:highlighted];
}
Easy as pie.

Related

UINaviagationBar ignoring background color settings [duplicate]

This question already has an answer here:
Unable to set barTintColor with custom RGBa in iOS7
(1 answer)
Closed 3 years ago.
ios 13, objective-c, can't get the navigation bar background color set.
I've tried setting the color via the UI in interface builder, and several examples off the web (and here).
UINavigationBarAppearance* navBarAppearance = [self.navigationController.navigationBar standardAppearance];
navBarAppearance.backgroundColor = [UIColor colorWithRed:172.0f green:193.0f blue:197.0f alpha:1.0f];
I would expect the above code to set the background color, but it's still white.
change this:
navBarAppearance.backgroundColor = [UIColor colorWithRed:172.0f green:193.0f blue:197.0f alpha:1.0f];
to this:
navBarAppearance.backgroundColor = [UIColor colorWithRed:172.0f/255.0f green:193.0f/255.0f blue:197.0f/255.0f alpha:1.0f];

UIView.animate does not work on background color [duplicate]

This question already has answers here:
How to animate the background color of a UILabel?
(11 answers)
Closed 6 years ago.
I have multiple labels with lightGray background color, and I want to animate their colors to UIColor.clear:
UIView.animate(withDuration: 5.0, animations: {
self.titleLabel.backgroundColor = UIColor.clear
self.dateLabel.backgroundColor = UIColor.clear
self.categoryLabel.backgroundColor = UIColor.clear
})
But color changes instantly! Why?
Well, I dont know the reason behind this why Apple didnt allow to animate the background color change, but here is a solution provided in another stackoverflow's post, which is as follows:
#import <QuartzCore/QuartzCore.h>
...
theLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;
[UIView animateWithDuration:2.0 animations:^{
theLabel.layer.backgroundColor = [UIColor greenColor].CGColor;
} completion:NULL];
Another hack is that you make a UIView behind UILabel, and give UILabel clear color, this way you can animate the color change of UIView

How do I make a UISwitch under iOS 7 not take the background colour of the view behind it?

It looks like this whenever off:
While I'd prefer more of a grey background. Do I really have to use a UIImageView?
Here is how I changed the fill color of my iOS7 UISwitch.
First you need to import QuartzCore.
#import <QuartzCore/QuartzCore.h>
Then set the background color and round the UISwitch's corners.
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
mySwitch.backgroundColor = [UIColor redColor];
mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
[self addSubview:mySwitch];
This will give you a UISwitch with a custom off (background) color.
Hope this helps someone:)
You can set the setOnTintColor property of your UISwitch to the color you desire.
You can also set this for the switch in Interface Builder. Just set the background colour of the UISwitch to whatever colour you want (white, in the example below), then set a User Defined Runtime Attribute of layer.cornerRadius = 16:
There's no API support for changing the off fill color of a UISwitch.
Adjusting the tintColor will only affect the outline, and adjusting the backgroundColor will affect the whole frame, including the parts outside the rounded bounds.
You either have to place a properly shaped opaque UIView behind it or - easier - use a custom open source implementation, such as MBSwitch, which allows you to set the off fill color.
You can also use an image as background, using the [UIColor colorWithPatternImage];
mySwitch.onTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"toggle-bg-on"]];
mySwitch.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"toggle-bg-off"]];
Adding to Barry Wyckoff solution : set tint color also
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
mySwitch.backgroundColor = [UIColor redColor];
mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
mySwitch.tintColor = [UIColor redColor];
[self addSubview:mySwitch];

How can I get the iOS 7 default blue color programmatically?

I'm creating custom elements in my app and want to match the look and feel of the new iOS. iOS 7 introduced to us a very common lighter blue color, the default color or tint for several elements, including the system button, segmented control, etc. They've made it easy to select the color using IB, as seen here:
However, I haven't found how to easily access the color programmatically. I checked out the UIColor documentation, and there doesn't seem to be any accessor for the blue system color in the class itself.
Here's my question: does a simple accessor exist for this color? [UIColor ?] or something like it? If not, does someone know the exact RGB values for that color?
Use self.view.tintColor from a view controller, or self.tintColor from a UIView subclass.
It appears to be [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0].
iOS 7 default blue color is R:0.0 G:122.0 B:255.0
UIColor *ios7BlueColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
According to the documentation for UIButton:
In iOS v7.0, all subclasses of UIView derive their behavior for tintColor from the base class. See the discussion of tintColor at the UIView level for more information.
Assuming you don't change the tintColor before grabbing the default value, you can use:
self.view.tintColor
Here is a simple method to get the default system tint color:
+ (UIColor*)defaultSystemTintColor
{
static UIColor* systemTintColor = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UIView* view = [[UIView alloc] init];
systemTintColor = view.tintColor;
});
return systemTintColor;
}
Hex Color code
#007AFF
and you need this libary
https://github.com/thii/SwiftHEXColors
ps. iOS, Swift
swift 4 way:
extension UIColor {
static let system = UIView().tintColor!
}
Native extension with predefined system colors gives what you're looking for:
// System colors
extension UIColor {
/* Some colors that are used by system elements and applications.
* These return named colors whose values may vary between different contexts and releases.
* Do not make assumptions about the color spaces or actual colors used.
*/
...
#available(iOS 7.0, *)
open class var systemBlue: UIColor { get }
...
}
You can use it directly:
myView.tintColor = .systemBlue
Get the color automatically by using this code:
static let DefaultButtonColor = UIButton(type: UIButtonType.System).titleColorForState(.Normal)!
The UIWindow.tintColor method wasn't working for me in iOS8 (it was still black), so I had to do this:
let b = UIButton.buttonWithType(UIButtonType.System) as UIButton
var color = b.titleColorForState(.Normal)
This gave the proper blue tint seen in a UIBarButtonItem
From iOS 7 there is an API and you can get (and set) the tint color with:
self.view.tintColor
Or if you need the CGColor:
self.view.tintColor.CGColor
In many cases what you need is just
[self tintColor]
// or if in a ViewController
[self.view tintColor]
or for swift
self.tintColor
// or if in a ViewController
self.view.tintColor
Please don't mess with view.tintColor or extensions, but simply use this:
UIColor.systemBlue
while setting the color you can set color like this
[UIColor colorWithRed:19/255.0 green:144/255.0 blue:255/255.0 alpha:1.0]
Adding a category to UIColor the following way will make it available to you anytime you need it or even change its definition accross your code:
#interface UIColor (iOS7Colors)
+ (instancetype)iOS7blueColor;
#end
#implementation UIColor (SpecialColors)
+ (instancetype)iOS7blueColor;
{
return [UIColor colorWithRed:0.0f green:0.22f blue:122.0/255.0 alpha:1.0f];
}
Once you import the Category in your code you can call the color by using:
UIColor *myBlueColor = [UIColor iOSblueColor];

Fill the UIButton with a color when touched up inside [duplicate]

This question already has answers here:
iOS - Setting UIButton background color only colors the corners
(3 answers)
Closed 9 years ago.
I want to fill the UIButton with color when it is touched. The method can be implemented in the action method
- (IBAction)bocClick:(UIButton *)sender {
[sender setBackgroundColor:[UIColor blackColor]];
}
i get the button like below
this background is not enough for my need ,i need have an effect ,something like..
You're using a round rect button which is bit complex to customize.
You can create a custom type button and use a background image, or create a custom type button and round the corners and set the background colour of the layer yourself.
or you can also do with bellow:-
you can add UIView or Lable like this bellow example:-
- (IBAction)bocClick:(UIButton *)sender {
UILabel *lbl2=[[UILabel alloc]initWithFrame:_btn.bounds];
lbl2.text=#"Button";
lbl2.textAlignment=UITextAlignmentCenter;
lbl2.font=[UIFont boldSystemFontOfSize:16];
lbl2.backgroundColor=[UIColor redColor];
lbl2.layer.cornerRadius = 5;
lbl2.layer.masksToBounds = YES;
[sender addSubview:lbl2];
}
OUTPUT IS:-
something similor:-
iOS - Setting UIButton background color only colors the corners
You just have to set the tintColorproperty:
[sender setTintColor:[UIColor redColor]];
So you get the highlight color when TouchUpInside. If you also want to change the background color it's more complex. Then you have to use Quartz
Only do this
UIButton * bocClick = [UIButton buttonWithType:UIButtonTypeCustom];
please try changing the button Type to UiButtonTypeCustom.
Please look at the image below.
hope it will help you.

Resources