ios - 访问系统。破坏性的红色按钮颜色

标签 ios swift uicolor

如何访问用于破坏性警报按钮样式的系统红色?

例如,用于按钮的默认样式蓝色可以在 Swift 中通过 let systemBlueColor = view.tintColor 访问,它对应于 UIColor(red: 0, green: 122,蓝色:255,阿尔法:1.0)

破坏性的红色似乎是由 UIColor(red: 255, green: 59, blue: 48, alpha: 1.0) 给出的,但是有没有办法以类似的方式访问它默认的 view.tintColor 方法?

我了解到 RGB 解释可能因设备/操作系统而异,因此我想访问与设备/操作系统无关的颜色版本。

最佳答案

UIColor 上有一个未记录的类方法,名为 _systemDestructiveTintColor,它将返回您需要的颜色:

let red = UIColor.performSelector("_systemDestructiveTintColor").takeUnretainedValue()

它返回一个非托管对象,您必须对其调用 .takeUnretainedValue(),因为颜色所有权尚未转移到我们自己的对象。

与任何未记录的 API 一样,尝试使用此方法时应谨慎:

swift 5:

if UIColor.responds(to: Selector(("_systemDestructiveTintColor"))) {
    if let red = UIColor.perform(Selector(("_systemDestructiveTintColor")))?.takeUnretainedValue() as? UIColor {
        // use the color
    }
}

以前的 Swift 版本:

if UIColor.respondsToSelector("_systemDestructiveTintColor") {
    if let red = UIColor.performSelector("_systemDestructiveTintColor").takeUnretainedValue() as? UIColor {
        // use the color
    }
}

可以在 UIColor header 中找到这种颜色和其他颜色.

关于ios - 访问系统。破坏性的红色按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36139462/

相关文章:

ios - 从 NIB 加载自定义类 UIView (IBOutlets nil)

ios - 调用将 UIViewController 插入闭包内部的函数,就好像它在外面一样

swift - 从 Coredata 加载数据时,我在控制台中收到警告

ios - UIImage 转换为数据并返回更改大小?

iphone - 我正在努力比较我制作的 UIcolor

javascript - 在我的 iOS 应用程序中集成 jquery-javascript 项目

ios - 使用 CLLocation 和/或 Mapkit 从坐标查找企业名称信息

ios - 我在 swift 中面临一个问题 tabBar 菜单和侧边栏菜单

iphone - 如何从 UIColor 获取色调、饱和度和亮度?

ios - 是否可以从同一项目的 Swift 类调用 Objective-C 应用程序委托(delegate)方法?