Last active
August 5, 2023 17:18
-
-
Save ingconti/be5417e253b16d99bd09128c26dd94ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// CustomToolBar | |
// | |
// Created by ing.conti on 05/08/23. | |
// | |
// some stuff from nice exmple: | |
// https://stackoverflow.com/questions/66134755/swiftui-extract-toolbarcontent-to-its-own-var | |
// with some buttons and call back. | |
import SwiftUI | |
struct ContentView: View { | |
// stright form xcode template, BUT add a NavigationStack ! | |
var body: some View { | |
NavigationStack { | |
VStack { | |
Image(systemName: "globe") | |
.imageScale(.large) | |
.foregroundStyle(.tint) | |
Text("Hello, world!") | |
} | |
// add here.. NOT on NavigationStack! | |
.toolbar{ CustomToolBarContent(t1: "Hoo", t2: "Hii", callback: doStuff ) } | |
} | |
} | |
private func doStuff(_ index: Int){ | |
print(index) | |
} | |
} | |
// move in appropriate file.. | |
typealias CallBack = ( (Int)->() )? | |
struct CustomToolBarContent: ToolbarContent { | |
internal init(t1: String, t2: String, callback: CallBack = nil) { | |
self.t1 = t1 | |
self.t2 = t2 | |
self.callback = callback | |
} | |
private let t1: String | |
private let t2: String | |
private let callback: CallBack | |
var body: some ToolbarContent { | |
ToolbarItem(placement: .principal) { | |
Text(t1) | |
} | |
ToolbarItem(placement: .navigationBarTrailing) { | |
Text(t2) | |
} | |
ToolbarItem(placement: .navigationBarTrailing) { | |
Button("Do 1") { | |
callback?(1) | |
} | |
} | |
ToolbarItem(placement: .navigationBarTrailing) { | |
Button("Do 2") { | |
callBack?(2) | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment