ios 自定义颜色 Swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38558361/
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-08-31 09:45:39  来源:igfitidea点击:

Custom Color Swift

iosswift

提问by Lampros Tzanetos

I am currently working on an app for kids where they press buttons and the background color changes. I have programmed the app for iOS 10 using Xcode 8.2. Unfortunately I can't find a way to set my own custom colors for the backgrounds and I am stuck with the presets... Can anybody help?

我目前正在为孩子们开发一个应用程序,他们按下按钮并且背景颜色会发生变化。我已经使用 Xcode 8.2 为 iOS 10 编写了应用程序。不幸的是,我找不到一种方法来为背景设置我自己的自定义颜色,而且我被预设困住了......有人可以帮忙吗?

回答by Shaan Singh

You can set the background color of a view using:

您可以使用以下方法设置视图的背景颜色:

self.view.backgroundColor = UIColor(red: 1.00, green: 1.00, blue: 1.00, alpha: 1.00)

In Swift 3, you can use default colors like this: UIColor.blue(). If you want to customize your color, you can use UIColor with specific red, green, and blue values (or any other number of parameters). You convert a color from HEX to UIColor by using any number of available online tools (like: http://uicolor.xyz/#/hex-to-ui). You can also write a Swift extension to do this in your code.

在斯威夫特3,你可以使用默认的颜色是这样的:UIColor.blue()。如果要自定义颜色,可以使用具有特定红色、绿色和蓝色值(或任何其他数量的参数)的 UIColor。您可以使用任意数量的可用在线工具(例如:http: //uicolor.xyz/#/hex-to-ui)将颜色从 HEX 转换为 UIColor 。您还可以编写一个 Swift 扩展来在您的代码中执行此操作。

回答by Dan Levy

If you have a custom color that isn't "blue", "red", etc, you can use the UIColor's RGB function. R G and B are out of 255

如果您的自定义颜色不是“蓝色”、“红色”等,则可以使用 UIColor 的 RGB 函数。RG 和 B 都在 255 之外

UIColor(red: 120/255.0, green: 150/255.0, blue: 200/255.0, alpha: 1)

回答by Daniel T.

I suggest you check the documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIColor_Class/#//apple_ref/doc/uid/TP40006892-CH3-SW17

我建议您查看文档:https: //developer.apple.com/library/ios/documentation/UIKit/Reference/UIColor_Class/#//apple_ref/doc/uid/TP40006892-CH3-SW17

There are 6 different initializers for a UIColor object, so no matter how you want to make your color, its got you covered.

UIColor 对象有 6 个不同的初始值设定项,因此无论您想如何制作颜色,它都能满足您的需求。