16

我知道对于类的元素,UIButton它们UIBarButtonItem会自动假定window.tintColor为主要颜色,如果我在应用程序中随时为窗口设置新的 tintColor,则会立即更改。

我想知道是否有任何方法可以使UILabel元素遵循相同的模式,一旦创建它们就会自动假定其默认颜色,window.tintColor并且如果window.tintColor在我的应用程序运行时中的任何时间更改也会导致UILabel自动更改 tintColour?

我希望这是有道理的。

4

2 回答 2

35

UILabels是 的子类UIView,因此当您在 iOS 7 中运行时,它们将具有一个tintColor属性,并且如果它们的 tint color 设置为nil(这是默认值),它们将从其父视图继承该颜色。

来自Apple 的文档

默认情况下,视图的色调颜色为 nil,这意味着视图使用其父级的色调。这也意味着当您向视图询问其色调颜色时,它总是返回一个颜色值,即使您没有设置一个。

但是,您还问“是否在我的应用程序运行时中的任何时候更改 window.tintColor 也会导致自动更改 UILabel tintColour?” Apple 建议您不要在屏幕上显示项目时更改色调颜色:

通常,最好在视图离开屏幕时更改视图的色调颜色。

我猜这是因为无法保证所有各种 UI 元素都会检测到 tintColor 更改并更新其可见视图。但是,如果您想在屏幕上进行更新,UIView文档建议了一种解决方法:tintColorUILables

要在此属性更改时刷新子视图渲染,请覆盖 tintColorDidChange 方法。

因此,只要确保调用tintColorDidChange当前屏幕上的任何视图,其色调颜色应在tintColor其父视图更改时更新。

但是你的为什么不UILabel更新他们的颜色呢?

因此,以上内容可以帮助您设置和更新各种tintColor's,但您没有看到任何效果 - 为什么?

嗯,这与 Apple 设计 Tint 所要指示的内容有关。从人机界面指南

颜色为用户提供了强烈的交互性视觉指示

苹果摆脱了交互元素周围的边框和渐变,取而代之的是颜色——特别是颜色tintColor。背后的整个想法tintColor是用户可以点击的东西得到它,而他们不能点击的东西却没有。

UILabel不是交互式元素-它是文本描述-因此Apple允许您tintColor在其上设置 a (因为任何UIView具有 a tintColor)但设置tintColor不会改变它的绘制方式。

那你该怎么办?首先,请注意,让更多的按钮呈现淡色对于您的应用程序来说可能是一个糟糕的 UI 选择 - iOS 7 用户和 Apple 应用程序审阅者都希望遵循这些规则。

那么你是否被迫保持你UILabel的颜色自由呢?

不——特别是如果你做的事情“正确”。苹果解释说:

在内容区域中,仅在必要时添加按钮边框或背景。条形图、操作表和警报中的按钮不需要边框,因为用户知道这些区域中的大多数项目都是交互式的。另一方面,在内容区域中,按钮可能需要边框或背景来将其与其他内容区分开来。

我建议您考虑应用程序的 UI。如果你真的希望你的非交互元素与tintColor你的交互元素一样,那么确保你使用更多的东西,比如边框或背景颜色,这样你的用户(和 Apple 应用程序审查者)就知道什么是可点击的,什么是不可点击的.

至于您应该如何更新颜色,您可以手动将textColor属性设置为您想要的任何颜色,或者您需要创建自己的UILabel覆盖子类- (void)tintColorDidChange以在发送通知时更新textColor属性 - 允许您拥有aUILabel其文本更新以匹配tintColor其父级的。

我希望这有帮助!

于 2013-10-09T03:41:25.340 回答
0

发现上面的解释很有帮助——尤其是指向 tintColorDidChange() 的指针。然而,让它正确地工作一开始并没有奏效,但最终想出了一个可以做到的代码示例。基本上,我有一个包含图像和标签的单元格的 tableView。图像会随着 tintColor 的变化而更新,但标签不会 - 这看起来不正确。最好两者都改变​​或都不改变。下面的代码是针对 tableView 中的单元格的。这是用 Swift 4.2 编写的。

//
//  ImageLabelCell.swift
//    -- provides an example of changing "tintColor" of UILabel to behave like other elements.
//    -- made problem a bit more complex by having a "selected" cell be in inverse colors - so also deal with background.
//

import UIKit

enum CellTypes: Int, CaseIterable { case cell1, cell2, cell3, cell4

    // This type provides a demonstration of a way to associate different titles and images to populate the UILabel and UIImageView in the cell.
    var title: String {
        return "\(self)".uppercased()
    }

    var imageName: String {
        return "\(self)"
    }
}

class ImageLabelCell: UITableViewCell {

    @IBOutlet weak var lblString: UILabel?
    @IBOutlet weak var imgView: UIImageView!

    fileprivate var type = CellTypes.cell1
    fileprivate var cellSelected = false

}

extension ImageLabelCell {

    func getFgBgColors() -> (UIColor, UIColor) {    // get foreground, background colors
        let white = UIColor.white
        var useTint = UIColor.blue  // Use your app color here.  Just ensures useTint is not nil.
        if let tint = tintColor {
            useTint = tint
        }
        if cellSelected {
            return (white, useTint)     // Selected cell is white on colored background
        } else {
            return (useTint, white)     // Unselected cell is colored on white background
        }
    }

    func configureCell(type: CellTypes, andSelected selected: Bool) {
        // Save properties we may use again later
        self.type = type
        self.cellSelected = selected

        // Set label text and image
        lblString?.text = type.title
        imgView.image = UIImage(named: type.imageName)

        // Set colors
        let (fgColor, bgColor) = getFgBgColors()
        imgView.tintColor = fgColor
        self.contentView.backgroundColor = bgColor
        lblString?.textColor = fgColor
    }

    override func tintColorDidChange() {
        // This gets called when the program tint color changes to gray (or back again) such as when popups appear/disappear.
        let (fgColor, bgColor) = getFgBgColors()
        // NOTE: need to set text color and background color.  Imageview can take care of itself.
        lblString?.textColor = fgColor
        self.contentView.backgroundColor = bgColor
    }

}
于 2018-11-26T00:51:50.927 回答