简体   繁体   中英

Embed view as title inside SwiftUI navigation bar

I want to embed a switch into the navigation view like this: Image

In UIKit, this is very easy: add the switch right after the navigation bar loads by doing self.navigationItem.titleView = controllSwitch However, with SwiftUI I can't do that.

Here's my swiftui code:

struct NotesDetail: View {

    var body: some View {
        ZStack(alignment: .topLeading) {
            Rectangle()
            .foregroundColor(note.color)
            .opacity(0.25)
                .edgesIgnoringSafeArea(.all)
            Text("Hello World")
            }
            .edgesIgnoringSafeArea(.bottom)
        .padding()
        }
    }
}

Here is what you want:

struct NotesDetail: View {

    @State private var toggleNow = true

    var body: some View {
        NavigationView {
            Text("Toggle")
                .navigationBarItems(trailing:
                    HStack {
                        Toggle(isOn: $toggleNow) {
                            Text("")
                        }

                    }
                    .padding(.trailing, UIScreen.screenWidth/2.5)
            )
        }
    }
}

extension UIScreen{
    static let screenWidth = UIScreen.main.bounds.size.width
    static let screenHeight = UIScreen.main.bounds.size.height
    static let screenSize = UIScreen.main.bounds.size
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM