0

I want to add buttons on the title like here.

enter image description here

I did this with SwiftUI NavigationView. However, I had to add SearchController and I added NavigationViewController as custom.

Custom NavigationViewController

The searchController works correctly, but I could not add buttons above the search controller.

struct CustomNavigationView: UIViewControllerRepresentable {
    
    let navigationActon = NavigationBarItemActions()
    var view: MainView
    func makeCoordinator() -> Coordinator {
        return CustomNavigationView.Coordinator(parent: self)
    }
    
    
    func makeUIViewController(context: Context) -> UINavigationController {
        let childView = UIHostingController(rootView: view)
        let controller = UINavigationController(rootViewController: childView)
        controller.navigationBar.topItem?.title = "Coming Movie"
        controller.navigationBar.prefersLargeTitles = true
        
        let searchController = UISearchController()
        searchController.searchBar.placeholder = "Movies"
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.delegate = context.coordinator
        
        controller.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "bell"), style: .plain, target: nil, action: #selector(navigationActon.notificationButtonTapped))//How Can I this button ?
        
        controller.navigationBar.topItem?.hidesSearchBarWhenScrolling = false
        controller.navigationBar.topItem?.searchController = searchController
        
        
        
        return controller
    }
    func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {
        
    }
    
    class Coordinator: NSObject, UISearchBarDelegate {
        var parent: CustomNavigationView
        
        init(parent: CustomNavigationView) {
            self.parent = parent
        }
        
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            print(searchText)
        }
        
        func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
            
        }
    }
    class NavigationBarItemActions {
        @objc func notificationButtonTapped() {
            
        }
    }
}

Use of

CustomNavigationView(view: MainView())
                                .ignoresSafeArea()

How can I add buttons to the places I marked in the image below?

enter image description here

Ufuk Köşker
  • 1,288
  • 8
  • 29

1 Answers1

0

I solved my problem. After adding Custom Navigation I added .navigationBarItems to MainView.

MainView

struct MainView: View {
    @State var searchText: String = ""
    
    var body: some View {
        ScrollView {
            VStack {
                ForEach(0 ..< 555) { item in
                    NavigationLink(
                        destination: TextDetailView(),
                        label: {
                            Text("Hello, World!")
                        })
                    
                }
            }
            .navigationBarItems(
                leading:
                    Button(action: {}) {
                        Text("Button 1")
                    }, trailing:
                        Button(action: {}) {
                            Text("Button 2")
                        }
            )
        }
        
    }
}

enter image description here

Ufuk Köşker
  • 1,288
  • 8
  • 29