tintColor in SwiftUI

栏目: IT技术 · 发布时间: 6年前

内容简介:In the previous article we talk abouttintColor. A small but useful property in UIKit. In this article, we will look intoJust like tint color, most SwiftUI views rely onIf we don't set it, the accent color is the system default (blue).

In the previous article we talk abouttintColor. A small but useful property in UIKit. In this article, we will look into accentColor —an equivalent property in SwiftUI. Let's see whether it can compete with tintColor .

Just like tint color, most SwiftUI views rely on accentColor as their theming mechanic.

The system uses the accent color for common controls and containers.
struct ContentView: View {
    @State var progress: CGFloat = 0.5

    var body: some View {
        VStack(spacing: 20) {
            Button("Button") {

            }
            Slider(value: $progress)
        }
    }
}

If we don't set it, the accent color is the system default (blue).

tintColor in SwiftUI
Button with pink as accent color

SwiftUI View has a method accentColor(_ accentColor: Color?) , which will set the accent color for the view and all of its children.

Sets the accent color for the view and the views it contains.
var body: some View {
    VStack(spacing: 20) {
        Button("Button 1") {

        }
        Button("Button 2") {

        }
        Slider(value: $progress)
    }.accentColor(.pink)
}
tintColor in SwiftUI
Sets the accent color for the view and the views it contains

Every view inherits accent color from its ancestor. If you want to opt-out of this, you can do that by setting another accent color to that view.

var body: some View {
    VStack(spacing: 20) {
        Button("Button 1") {

        }.accentColor(nil)

        Button("Button 2") {

        }.accentColor(.green)

        Slider(value: $progress)
    }.accentColor(.pink)
}
tintColor in SwiftUI
Opt-out from parent accent color by set their own accent color.

Global accent color

As I mentioned before, the accent color is inherited from an ancestor. So you can apply the same accent color for every view by set accentColor to an ancestor of all views.

This might vary on your implementation, but for most cases, it should be set in AppDelegate or SceneDelegate .

Remove .accentColor(.pink) from ContentView .

struct ContentView: View {
    @State var progress: CGFloat = 0.5

    var body: some View {
        VStack(spacing: 20) {
            Button("Button 1") {

            }.accentColor(nil)

            Button("Button 2") {

            }.accentColor(.green)

            Slider(value: $progress)
        }.accentColor(.pink)
    }
}

And set it where you initiate ContentView .

let contentView = ContentView().accentColor(.pink)
window.rootViewController = UIHostingController(rootView: contentView)

How to detect accent color change?

In UIKit, we can detect a change and adjust our view accordingly by override tintColorDidChange() method. In general, the way SwiftUI use to adapt to change is by using Combine Framework and Built-in Data binding, e.g., @State , @Binding , and @ObservedObject , but accentColor is just a static var . At first, I worry that it won't adapt to accent color changes, turn out it can adapt to the change magically.

We start by creating a custom view that shows a rectangle of color from the accent color.

struct MyCustomView: View {
    var body: some View {
        Rectangle().frame(width: 100, height: 100).foregroundColor(.accentColor)
    }
}

In ContentView .

struct ContentView: View {
    var body: some View {
        ExtractedView().accentColor(.pink)
    }
}
tintColor in SwiftUI
Custom view with accent color

So, a custom view can adapt to the accent color just by using it. Let's try to change accent color dynamically to confirm this behavior.

We set up a view with four buttons that change accent color when click.

struct ContentView: View {
    @State var color: Color = .blue

    var body: some View {
        VStack {
            ExtractedView().accentColor(color)
            HStack {
                Button("Blue") {
                    self.color = .blue
                }
                Button("Red") {
                    self.color = .red
                }
                Button("Yellow") {
                    self.color = .yellow
                }
                Button("Green") {
                    self.color = .green
                }
            }
        }
    }
}

It works flawlessly.

tintColor in SwiftUI
Custom view with accent color can adapt to changes

Accent color got everything tint color has, and I think it is far easier to use. I still not sure how a view can listen to accentColor changes. I expect it to expose this value through @Environment property, something like:

@Environment(\.accentColor) var accentColor

But, accentColor can work without any of this. This is quite good, but also a mystery to me. If you know how this magic works, please enlighten me. You can direct message me on Twitter .


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

沸騰15年

沸騰15年

林軍 / 商周出版 / 2010年09月19日 / NTD:430元

從一九九五年到二○○九年,中國互聯網崛起、發展和壯大。 在短短十五年間 產生了十五家市值超過十億的上市公司 這些前仆後繼的先行者 不但用網際網路創造了歷史,也改寫了自己的財富路徑。 這本關於中國互聯網產業歷史的書,脈絡清晰、生動鮮明地把一大批創業者的形象和他們的動人故事呈現在讀者眼前,值得一讀。 ——中國互聯網協會理事長、中國科協副主席 胡啟?? 林軍這本......一起来看看 《沸騰15年》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具