article

Tuesday, May 25, 2021

SwiftUI how to add button to navigation bar

SwiftUI how to add button to navigation bar

Ho to add buttons and images to navigation bar in swiftUI NavigationView

 
//
//  ContentView.swift
//  Testapp
//
//  Created by Cairocoders
//

import SwiftUI


struct ContentView: View {
    
    @State var showAlert: Bool = false
    @State var msg: String = ""
    
    var body: some View {
        NavigationView {
            Text("Cairocoders - tutorial101.blogspot.com")

            .navigationBarTitle("Tutorials")
            .navigationBarItems(trailing:
                HStack {
                        Button(action: {
                            self.showAlert = true
                            self.msg = "Reload button pressed..."
                        }) {
                            Text("Reload")
                        }
                        
                        Button(action: {
                            self.showAlert = true
                            self.msg = "Edit button pressed..."
                        }) {
                            Image(systemName: "person.crop.circle").imageScale(.large) //SF Symbol
                        }
                }
            )
        }
        .alert(isPresented: $showAlert, content: {
            Alert(title: Text(self.msg))
        })
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Related Post