ios - UIAppearance 色调颜色优先于 Storyboard 色调颜色

标签 ios storyboard uiappearance

我使用 Interface Builder 创建了一个简单的 View Controller ,其中包含 UIImageView 和 UIButton。在它们两个上,我将 tintColor 设置为洋红色。

Interface Builder

在 AppDelegate 上,我将 UIView.appearance.tintColor 设置为青色。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UIView.appearance.tintColor = UIColor.cyanColor;

    return YES;
}

运行应用程序时,我得到以下信息:

Simulator

这对我来说没有意义。我希望直接在 imageView 和按钮上指定的洋红色色调覆盖青色色调。

为什么会这样?

作为引用,我使用的是 Xcode 版本 11.2 (11B52)。

最佳答案

这表明 tintColor 属性未使用 UI_APPEARANCE_SELECTOR 进行修饰,因此不应在 UIAppearance 代理中使用它。

即使看起来有效,设置 UIView.appearance.tintColor 也会产生副作用。一个例子是 Storyboard的问题。我注意到的另一个情况是,在 iOS 13 中,UITableView 部分标题将填充设置的颜色。

鉴于这不是受支持的场景,很难确切说明为什么尝试覆盖 Storyboard上组件的 tintColor 不起作用。再次检查文档,我发现了一个可以解释它的注释:

iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.

我假设“storyboard”tintColor 将在加载 Storyboard 对象时设置,然后一旦将 View 插入到 View 层次结构中,UIKit 就会用“UIAppearance”tintColor 覆盖它。

解决方案

设置全局tintColor的正确方法似乎是在主UIWindow上设置它。根据UIView.h中的注释,tintColor是由superview继承的。这允许对所有 View 使用全局tintColor,并在需要时在本地覆盖它。从 Storyboard设置属性时,它按预期工作。

关于ios - UIAppearance 色调颜色优先于 Storyboard 色调颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58762917/

相关文章:

ios - 从 Storyboard 中获取 viewController,而不是使用 instantiateViewControllerWithIdentifier

iphone - 将 UIAppearance 与 UIBarButtonItem 的子类一起使用会导致无法识别的选择器被发送到 UINavigationButton

uitableview - -[UITableView _addScrollViewScrollObserver :] while using large title navigation ios 11 中的断言失败

objective-c - 包含在多个类中时的 iOS 6 外观

ios - 导出发布版本 FlashBuilder 4.7

ios - OpenGL 更改坐标设置

ios - 如何设置 subview 的框架

ios - Main~.storyboard 文件,就像一个副作用

ios - 根据编译器优化,应用程序始终在不同点崩溃

ios - EXC_BAD_INSTRUCTION 按下按钮