xcode 如何在视图控制器的背景中设置自定义颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40191093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:29:00  来源:igfitidea点击:

How to set custom color in the view controller's background

iosswiftxcodebackground-color

提问by H. Ferrence

I want to set the background color of my mobile app to #B2B27A.

我想将我的移动应用程序的背景颜色设置为 #B2B27A。

I can get to View Controller and the Background color panel, but I cannot figure out how to set my own RGB color. Is that possible to do?

我可以访问视图控制器和背景颜色面板,但我不知道如何设置我自己的 RGB 颜色。那有可能吗?

回答by Rashwan L

Simply:

简单地:

self.view.backgroundColor = UIColor(red: 178/255, green: 178/255, blue: 122/255, alpha: 1)

The rgbvalues are equal to #B2B27A.

rgb值等于#B2B27A

Edit:
You can also add an hex/rgb value in your Storyboard: enter image description here

编辑:
您还可以在故事板中添加十六进制/RGB 值: 在此处输入图片说明

  1. Select your view
  2. Click background in attributes inspector
  3. Select the second menu item in the colors window
  1. 选择您的观点
  2. 单击属性检查器中的背景
  3. 选择颜色窗口中的第二个菜单项

回答by Jayesh Gyanchandani

create a NSObject class as below:

创建一个 NSObject 类,如下所示:

class color:NSObject
{
    class func UIColorFromRGB(rgbValue: UInt) -> UIColor {
        return UIColor(
            red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }
}

call this class func in you ViewController viewDidLoad() like below:

在您的 ViewController viewDidLoad() 中调用此类 func ,如下所示:

self.view.backgroundColor = color.UIColorFromRGB(0xB2B27A)