Home > AI > IOS > SwiftUI >

PreferenceKey

Example 1 to show its basic function.

import SwiftUI



struct ContentView: View {
    let messages: [String]

    var body: some View {
        NavigationView {
            List(messages, id: \.self) { message in
                Text(message)
            }
            .navigationBarTitle("Messages")
            .myNavigationBarTitle("Messages")
            .onPreferenceChange(MyNavigationBarTitleKey.self) { title in
                        print(title)
                    }
                
        }
    }
}



struct MyNavigationBarTitleKey: PreferenceKey {
    static var defaultValue: String = ""

    static func reduce(value: inout String, nextValue: () -> String) {
        value = nextValue()
    }
}

extension View {
    func myNavigationBarTitle(_ title: String) -> some View {
        self.preference(key: MyNavigationBarTitleKey.self, value: title)
    }
}

Example 2:


import SwiftUI



struct ContentView: View {
    let messages: [String]

    var body: some View {
       
        // 1
//        VStack {
//            Text("A")
//                .onPreferenceChange(MyPreferenceKey.self, perform: { value in
//                    print(value)
//                })
//            Text("B")
//                .onPreferenceChange(MyPreferenceKey.self, perform: { value in
//                    print(value)
//                })
//        }
//        .onPreferenceChange(MyPreferenceKey.self, perform: { value in
//            print(value)
//        })
//
//
//
//        // 2
//        VStack {
//            Text("A")
//                .setMyPrefereceKey(1)
//                .onPreferenceChange(MyPreferenceKey.self, perform: { value in
//                    print("Text A ", value)
//                })
//            Text("B")
//                .onPreferenceChange(MyPreferenceKey.self, perform: { value in
//                    print("Text B ", value)
//                })
//        }
//        .onPreferenceChange(MyPreferenceKey.self, perform: { value in
//            print("VStack ", value)
//        })
        
        
        // 3
        VStack {
            Text("A")
                .setMyPrefereceKey(1)
                .onPreferenceChange(MyPreferenceKey.self, perform: { value in
                    print("Text A ", value)
                })
            Text("B")
                .setMyPrefereceKey(2)
                .onPreferenceChange(MyPreferenceKey.self, perform: { value in
                    print("Text B ", value)
                })
        }
        .onPreferenceChange(MyPreferenceKey.self, perform: { value in
            print("VStack ", value)
        })
    }
}



struct MyPreferenceKey: PreferenceKey {
    static var defaultValue: Int = 0

    static func reduce(value: inout Int, nextValue: () -> Int) {
        // 1 keep the latest
        value = nextValue()
        
        // 2 keep the first
        
        // 3
//        value += nextValue()
        
    }
}

extension View {
    func setMyPrefereceKey(_ num: Int) -> some View {
        self.preference(key: MyPreferenceKey.self, value: num)
    }
}

Leave a Reply