I am trying to change the tab bar color in a view controller in XCode using swift. I have a hex that I matched up to an RGB value and I am trying to set that in this code. (Which does not work)
let color = UIColor(red: 41, green: 40, blue: 39, alpha: 1.0)
UITabBar.appearance().barTintColor = color
However this code does:
UITabBar.appearance().barTintColor = UIColor.whiteColor()
Can anyone explain why this doesn't work, and what I can do to fix it?
To use RGB values, just divide them by 255.0. This will produce a float value between 0 and 1.
let color = UIColor(red: 41.0/255.0, green: 40.0/255.0, blue: 39.0/255.0, alpha: 1.0)
It doesn't work because all of your RGB components are greater than 1, which is the maximum available value per-channel. You're probably thinking of the color channels as bytes, but that wouldn't scale to varying color bit depths. (For example, it was common to render to RGB565, not RGBA8888 in early versions of iOS. And you can probably expect Apple to make screens with 16-bit accuracy the norm, in the near future.) Floats from 0 to 1 are employed, to divorce the bit depth from the color representation.
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIColor_Class/index.html#//apple_ref/occ/instm/UIColor/initWithRed:green:blue:alpha:
iOS 10 Swift 3.0
If you don't mind to use swift frameworks then us UINeraida to change Tabbar background as UIColor or HexColor or UIImage and change complete forground color.
For UITabBar
neraida.tabbar.background.color.uiColor(UIColor.orange, isTranslucent: false, viewController: self)
//change tab bar tint color //(select,unselect)
neraida.tabbar.foreground.color.uiColor((UIColor.white,UIColor.green), viewController: self)
//set Background Image for tab bar
neraida.tabbar.background.image("background", isTranslucent: false, viewController: self)
This way worked for me:
tabBarController?.tabBar.backgroundColor = .red
Related
I'm working on a Cocoa application and I find that as long as the font color of NSTextField is set to NSColor.controlTextColor, the font will change according to the background color of NSTextField.
For example, when I set the background color to white, the font becomes black.
But when I set the background color to black, the font turns white.
I want to define an NSColor to achieve the same effect. How to achieve it?
If you want to pass in any color and then determine which text color would be more ideal - black or white - you first need to determine the luminance of that color (in sRGB). We can do that by converting to grayscale, and then checking the contrast with black vs white.
Check out this neat extension that does so:
extension NSColor {
/// Determine the sRGB luminance value by converting to grayscale. Returns a floating point value between 0 (black) and 1 (white).
func luminance() -> CGFloat {
var colors: [CGFloat] = [redComponent, greenComponent, blueComponent].map({ value in
if value <= 0.03928 {
return value / 12.92
} else {
return pow((value + 0.055) / 1.055, 2.4)
}
})
let red = colors[0] * 0.2126
let green = colors[1] * 0.7152
let blue = colors[2] * 0.0722
return red + green + blue
}
func contrast(with color: NSColor) -> CGFloat {
return (self.luminance() + 0.05) / (color.luminance() + 0.05)
}
}
Now we can determine whether we should use black or white as our text by checking the contrast between our background color with black and comparing it to the contrast with white.
// Background color for whatever UI component you want.
let backgroundColor = NSColor(red: 0.5, green: 0.8, blue: 0.2, alpha: 1.0)
// Contrast of that color w/ black.
let blackContrast = backgroundColor.contrast(with: NSColor.black.usingColorSpace(NSColorSpace.sRGB)!)
// Contrast of that color with white.
let whiteContrast = backgroundColor.contrast(with: NSColor.white.usingColorSpace(NSColorSpace.sRGB)!)
// Ideal color of the text, based on which has the greater contrast.
let textColor: NSColor = blackContrast > whiteContrast ? .black : .white
In this case above, the backgroundColor produces a contrast of 10.595052467245562 with black and 0.5045263079640744 with white. So clearly, we should use black as our font color!
The value for black can be corroborated here.
EDIT: The logic for the .controlTextColor is going to be beneath the surface of the API that Apple provides and beyond me. It has to do with the user's preferences, etc. and may operate on views during runtime (i.e. by setting .controlTextColor, you might be flagging a view to check for which textColor is more legible during runtime and applying it).
TL;DR: I don't think you have the ability to achieve the same effect as .controlTextColor with an NSColor subclass.
Here's an example of a subclassed element that uses its backgroundColor to determine the textColor, however, to achieve that same effect. Depending on what backgroundColor you apply to the class, the textColor will be determined by it.
class ContrastTextField: NSTextField {
override var textColor: NSColor? {
set {}
get {
if let background = self.layer?.backgroundColor {
let color = NSColor(cgColor: background)!.usingColorSpace(NSColorSpace.sRGB)!
let blackContrast = color.contrast(with: NSColor.black.usingColorSpace(NSColorSpace.sRGB)!)
let whiteContrast = color.contrast(with: NSColor.white.usingColorSpace(NSColorSpace.sRGB)!)
return blackContrast > whiteContrast ? .black : .white
}
return NSColor.black
}
}
}
Then you can implement with:
let textField = ContrastTextField()
textField.wantsLayer = true
textField.layer?.backgroundColor = NSColor.red.cgColor
textField.stringValue = "test"
Will set your textColor depending on the layer's background.
i'm using The library SVProgressHUD for my project to show a spinning circle loader:
SVProgressHUD.setBackgroundColor(UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.3))
SVProgressHUD.setForegroundColor(UIColor.white)
SVProgressHUD.show()
my question is how do i make the background to full screen ?
and how do i block the presses in the background while it is displaying ?
the library:
https://github.com/SVProgressHUD/SVProgressHUD
You have to set background layer color to color you need. But don't forget, if you want to change background layer color, you also have to set default mask type as .custom
SVProgressHUD.setDefaultMaskType(.custom)
SVProgressHUD.setBackgroundLayerColor(.black) // your custom color
If you want to know how to customize other properties, have a look
here.
I've found a serious swift-bug in SpriteKit while working with SKSpriteNodes and their colors.
This code works fine on all iPhones beside the iPhone 5S:
var color1 = UIColor(red: 123/255, green: 123/255, blue: 123/255, alpha: 1)
var color2 = UIColor(red: 123/255, green: 123/255, blue: 123/255, alpha: 1)
var sprite = SKSpriteNode(color: color1, size: CGSizeMake(100, 100))
if(sprite.color == color2){
println("Same color")
}
As you see, the two colors are the absolut same. But on the iPhone 5S simulator, the if isn't called.
Has somebody else the same problem and can provide a solution?
According to the documentation here:
Sprite Kit works only with solid colors. For best results, use the
preset colors provided by the platform class or a custom color defined
in the RGBA device color space.
As a result somehow the SKSpriteNode has made some changes to the color parameter in the init function. You can see it if you call encode:
sprite.color.encode() // 140,646,370,382,768
color1.encode() // 140,646,367,110,928
If you use predefined color values, then your problem goes away:
var color3 = UIColor.blueColor()
var sprite3 = SKSpriteNode(color: color3, size: CGSizeMake(100, 100))
sprite3.color == color3 // true
You are comparing pointer values, not the actual color. Seeing that these are UIColor instances, you have to compare them using isEqual (showing ObjC code as I don't know what it looks like in Swift - or perhaps Swift is in fact using isEqual behind the scenes):
if ([sprite.color isEqual:color2])
If implemented correctly by UIColor this will compare the actual color values rather than the pointers.
Is there a way to set the UIView background color with Swift?
I know that in Objective-C, you would use self.view.backgroundColor = [UIColor redColor];, but that does not work the same way in Swift. I have looked around and because Swift is only about a week old, I cannot find an answer.
Does anyone have any suggestions?
self.view.backgroundColor = UIColor.redColor()
In Swift 3:
self.view.backgroundColor = UIColor.red
Try This, It worked like a charm! for me,
The simplest way to add backgroundColor programmatically by using ColorLiteral.
You need to add the property ColorLiteral, Xcode will prompt you with a whole list of colors in which you can choose any color. The advantage of doing this is we use lesser code, add HEX values or RGB. You will also get the recently used colors from the storyboard.
Follow steps ,
1) Add below line of code in viewDidLoad() ,
self.view.backgroundColor = ColorLiteral
and clicked on enter button .
2) Display square box next to =
3) When Clicked on Square Box Xcode will prompt you with a whole list of colors which you can choose any colors also you can set HEX values or RGB
4) You can successfully set the colors .
Hope this will help some one to set backgroundColor in different ways.
I see that this question is solved, but, I want to add some information than can help someone.
if you want use hex to set background color, I found this function and work:
func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor {
let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
let blue = CGFloat(rgbValue & 0xFF)/256.0
return UIColor(red:red, green:green, blue:blue, alpha:CGFloat(alpha))
}
I use this function as follows:
view.backgroundColor = UIColorFromHex(0x323232,alpha: 1)
some times you must use self:
self.view.backgroundColor = UIColorFromHex(0x323232,alpha: 1)
Well that was it, I hope it helps someone .
sorry for my bad english.
this work on iOS 7.1+
You can use the line below which goes into a closure (viewDidLoad, didLayOutSubViews, etc):
self.view.backgroundColor = .redColor()
EDIT Swift 3:
view.backgroundColor = .red
You can use this extension as an alternative if you're dealing with RGB value.
extension UIColor {
static func rgb(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1)
}
}
In Swift 4, just as simple as Swift 3:
self.view.backgroundColor = UIColor.brown
The response by #Miknash and #wolfgang gutierrez barrera was helpful to me. Only difference was I had to add rgbValue: to the function call.
UIColorFromHex(rgbValue: 0xA6D632,alpha: 1 ) like so
If you want to set your custom RGB color try this:
self.view.backgroundColor = UIColor(red: 20/255.0, green: 106/255.0, blue: 93/255.0, alpha: 1)
Don't forget to keep /255.0 for every color
In the Xcode 13, the shortcut ColorLiteral does not work anymore.
Now, you have to use this shortcut: #colorLiteral(
I'm trying to share my Scene Kit code across iOS and OS X, but the API calls that accept colors (and images) take either UIColor/UIImage or NSColor/NSImage depending on the platform. How do I create the right class in Swift without duplicating the code?
Use conditional compilation and type aliases:
#if os(OSX)
typealias Color = NSColor
typealias Image = NSImage
#else
typealias Color = UIColor
typealias Image = UIImage
#endif
Then use Color instead of UIColor or NSColor:
self.gameView!.backgroundColor = Color(red: 0, green: 0.2, blue: 0.5, alpha: 1)
Edit 2016-01-17: As DDPWNAGE noted above, Apple has created SKColor with basically the same definition.