我在一个按钮中有以下代码,它成功地改变了视图的背景颜色:
func bgcol() {
self.view.layer?.backgroundColor = NSColor.blue.cgColor
}我想将颜色改为自定义的rgb颜色,但我发现的所有颜色似乎都使用UI颜色,我想这只适用于iOS?
我也看过NSColor的文档,但作为一个完全的初学者,不幸的是我无法理解它:
谁能告诉我如何修改我的代码,以使用自定义的rgb颜色。
提前感谢大家。
更新:我现在有了这个代码--但是什么都没有改变:(:
func bgcol() {
let customColor = NSColor(red: 99.0, green: 0.0, blue: 0.0, alpha: 1.0)
self.view.layer?.backgroundColor = customColor.cgColor
}更新2:好的,在viewController.swift文件中,我在顶部附近有以下代码:
func bgcol() {
let customColor = NSColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
self.view.layer?.backgroundColor = customColor.cgColor}然后,在视图中我调用了LOAD()
bgcol()比方说,我想要的颜色是r220 g123 b45,我不知道如何将其设置为背景颜色?
这是一个全新的语法,让我抓狂了:(
发布于 2017-02-07 01:00:15
Command+click NSColor以了解其提供的功能:
/* Create NSCustomColorSpace colors that are compatible with sRGB colorspace; these variants are provided for easier reuse of code that uses UIColor on iOS. It's typically better to specify the colorspace explicitly with one of the above methods.
If red, green, blue, or saturation, brightness, or white values are outside of the 0..1 range, these will create colors in the extended sRGB (or for colorWithWhite:alpha:, extendedGenericGamma22GrayColorSpace) color spaces. This behavior is compatible with iOS UIColor.
*/
@available(OSX 10.9, *)
public /*not inherited*/ init(white: CGFloat, alpha: CGFloat)
@available(OSX 10.9, *)
public /*not inherited*/ init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
@available(OSX 10.9, *)
public /*not inherited*/ init(hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat)你可以使用这个初始化器:
let customColor = NSColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)https://stackoverflow.com/questions/42073023
复制相似问题