Setting custom color on UINavaigationBar - objective-c

Is it possible to set color of UINavigationBar with particular opacity and alpha? I've tried a lot things, but I still can't get what I want. The color is 7, 4, 43 for RGB and 0,4 for opacity and alpha.

I think this should work for you:
[navigationBar setBarTintColor:[UIColor colorWithRed:7.0/255.0 green:4.0/255.0 blue:43.0/255.0 alpha:0.4]];
And in Swift:
navigationBar.barTintColor = UIColor(red: 7.0, green: 4.0, blue: 43.0, alpha: 0.4)

Related

Why is the background color of my UIButton not affected in state config?

I am trying to configure UIButton disabled state using attribute inspector 'state config.' How can I configure the background color for the disabled state?
Try this:
#IBAction func buttonStateChanged(sender: UIButton) {
if(sender.selected){
sender.backgroundColor = UIColor(red: 1.0, green: 0.0, blue: 0.2, alpha: 1.0)
}else{
sender.backgroundColor = UIColor(red: 1.0, green: 0.0, blue: 0.2, alpha: 1.0)
}
}

Button border color - Objective C - Swift

I want to change my border color to a specific hex color(#21CE99), can you guys please help me?
I've already known how to change it to a basic color, for example:
_button.layer.borderColor = [UIColor greenColor].CGColor;
but I only need this color.
Thank you.
Answer
How to set the custome border color of UIView programmatically in swift
The correct answer for people using SWIFT 4 would be:
_button.layer.borderColor = UIColor(red: 33/255, green: 206/255, blue: 153/255, alpha: 1.0).cgColor
Built-In UIColors Presets or your own colors:
_button.layer.borderColor = UIColor.blue.cgColor
_button.layer.borderColor = [UIColor colorWithRed:33.0 / 255.0 green:206.0 / 255.0 blue:153.0 / 255.0 alpha:1.0].CGColor;
easier
_button.layer.borderColor = [UIColor colorWithRed:0x21 / 255.0 green:0xce / 255.0 blue:0x99 / 255.0 alpha:1.0].CGColor;

Core Graphics won't load all the way for certain buttons

I am coding a button background of a simple red gradient, which works when the height is less than 100, but will simply stop loading the gradient if the button is larger than that and I can't figure out why. I am using PaintCode (which up until now has worked like a charm) to draw the code, then I changed the size of the gradient from [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, 240, 120) cornerRadius: 4]; to [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) cornerRadius: 4]; because the size of the button changes between 4.5" and 5" devices. I am using other gradients with identical codes minus the color on other buttons and they work perfectly fine. It is only when the height is greater than 100 pts that I find a problem. Any idea what's wrong?
Here is the code that I'm using:
//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
//// Color Declarations
UIColor* lightRedColor = [UIColor colorWithRed: 1 green: 0.188 blue: 0.098 alpha: 1];
UIColor* darkRedColor = [UIColor colorWithRed: 0.812 green: 0.016 blue: 0.016 alpha: 1];
//// Gradient Declarations
NSArray* redGradientColors = [NSArray arrayWithObjects:
(id)lightRedColor.CGColor,
(id)[UIColor colorWithRed: 0.906 green: 0.102 blue: 0.057 alpha: 1].CGColor,
(id)darkRedColor.CGColor, nil];
CGFloat redGradientLocations[] = {0, 0.5, 1};
CGGradientRef redGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)redGradientColors, redGradientLocations);
//// Rounded Rectangle Drawing
UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) cornerRadius: 4];
CGContextSaveGState(context);
[roundedRectanglePath addClip];
CGContextDrawLinearGradient(context, redGradient, CGPointMake(120, 0), CGPointMake(120, 120), 0);
CGContextRestoreGState(context);
//// Cleanup
CGGradientRelease(redGradient);
CGColorSpaceRelease(colorSpace);
Edit: here is a screenshot of the button:
CGContextDrawLinearGradient(context, redGradient, CGPointMake(120, 0), CGPointMake(120, 120), 0);
Here you are specifying the height of the gradient area to draw, with an end point of y=120. This means your gradient will be 120 points high regardless of the code above it. Change the end point to be CGPointMake(120, self.bounds.size.height) and you should be fine.
It looks like the problem is [roundedRectanglePath addClip];. Specifically, that a clipping path has been specified on the current graphics context somewhere else in the code and either not been removed when it should be (missing call to CGContextRestoreGState) or it is set to a smaller size than it should be.
Check for clipping paths that have been added and not removed. Also check for superviews set to clip subviews and whose size is not correct.

Convert NSColor to RGB

I'm trying to convert an NSColor to RGB, but it seems to give an entirely incorrect result:
NSColor *testColor = [NSColor colorWithCalibratedWhite:0.65 alpha:1.0];
const CGFloat* components = CGColorGetComponents(testColor.CGColor);
NSLog(#"Red: %f", components[0]);
NSLog(#"Green: %f", components[1]);
NSLog(#"Blue: %f", components[2]);
NSLog(#"Alpha: %f", CGColorGetAlpha(testColor.CGColor));
I get back : red = 0.65 - green = 1.0 - blue = 0.0 and alpha is 1.0 - which results in an entirely different color. (It should be gray, now it's green).
Am I doing something wrong?
Extracting RGBA values from NSColor: (Swift 3)
let nsColor: NSColor = .red
let ciColor: CIColor = .init(color: nsColor)!
print(ciColor.red) // 1.0
print(ciColor.green) // 0.0
print(ciColor.blue) // 0.0
print(ciColor.alpha) // 1.0 // Or use nsColor.alphaComponent
NOTE: NSColor.blackColor().redComponent will crash the app, but the above code won't
I had the same problem when I wanted to convert a picked color to hexadecimal. NSColor components values was not correct. I managed to resolve my problem with your comment above.
Example in Swift:
let colorTest = NSColor.init(calibratedWhite: 0.65, alpha: 1.0)
let color = colorTest.usingColorSpace(NSColorSpace.deviceRGB) ?? colorTest
print(colorTest)
// NSCalibratedWhiteColorSpace 0.65 1
print(colorTest.colorSpace)
// Generic Gray colorspace
print("red: \(color.redComponent) green:\(color.greenComponent) blue:\(color.blueComponent)")
// red: 0.708725869655609 green:0.708725869655609 blue:0.708725869655609
You need to convert the color to an RGB color space using an NSColorSpace object first, then you can get the components using the various NSColor accessor methods
For a NSColor * color
CGFloat red = [color redComponent];
CGFloat green = [color greenComponent];
CGFloat blue = [color blueComponent];
I have used this in the past, and it worked for me.
NSColorSpace *colorSpace = [NSColorSpace sRGBColorSpace];
NSColor *testColor = [NSColor colorWithColorSpace:colorSpace components:SRGB];
CGFloat red = [testColor redComponent];
CGFloat green = [testColor greenComponent];
CGFloat blue = [testColor blueComponent];
You have to check the colorspace first
then if it's rgb you can use
CGFloat red = [testColor redComponent];
...
For grayscale you have to convert it differently
CGFloat red = [testColor whiteComponent];
CGFloat blue = [testColor whiteComponent];
CGFloat green = [testColor whiteComponent];
Here’s a safe Swift 5 SKColor extension for getting the RGB components of an NSColor or UIColor. (Note SKColor is just a typealias for one or the other based on the platform.)
public extension SKColor {
var sRGBAComponents: (red: CGFloat , green: CGFloat, blue: CGFloat, alpha: CGFloat) {
#if os(iOS)
let rgbColor = self // lolz no color conversion on iOS, but on iOS it'll respond to getRed(...) anyhow
#elseif os(macOS)
let rgbColor = usingColorSpace(.extendedSRGB) ?? SKColor(red: 1, green: 1, blue: 1, alpha: 1) // will return 'self' if already RGB
#endif
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
rgbColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
return (red: red, green: green, blue: blue, alpha: alpha)
}
}

How to set alpha of uiclearcolor?

I am currently doing this:
UIColor *myColor = [UIColor clearColor];
This is great but i would like to specify a certain alpha of "myColor". How would i do so?
If you have an existing color, you can return a new one with a specified alpha, like this:
- (void)setBackgroundColor:(UIColor *)color
{
self.backgroundColor = [color colorWithAlphaComponent:0.3f];
}
[UIColor clearColor] is, how should I put it?, clear!
It is a convenience class method returning a UIColor with alpha at zero.
If you want a color with a fractional amount of transparency:
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha
Or one of the other UIColor class methods.
You can use colorWithWhite:alpha like so:
[UIColor colorWithWhite:1.0 alpha:0.5];
Or if you want a specific color:
[UIColor colorWithRed:1.0 green:1.0 blue:0.3 alpha:0.72];
Check out the docs.
For Swift you can use:
UIColor.black.withAlphaComponent(0.5)
Or RGB Colors:
UIColor(red: 0/255.0 , green: 0/255.0 , blue: 0/255.0 , alpha: 0.5)
To answer that Question
UIColor *myColor = [[UIColor clearColor] colorWithAlphaComponent:0.3f];
I assume that you have "clearColor" as a valid UIColor;
#refer James O'Brien Solution;
If you prefer a simple and yet powerful solution in Swift then checkout HandyUIKit. Install it in your project using Carthage – then your life becomes easier:
import HandyUIKit
// creates a new UIColor object with the given value set
myColor.change(.alpha, to: 0.2)
There is also an option to apply a relative change:
// create a new UIColor object with alpha increased by 0.2
myColor.change(.alpha, by: 0.2)
I hope it helps!