如何在 UIViewControllerRepresentable Struct 中创建 UIBarButtonItem?

Posted

技术标签:

【中文标题】如何在 UIViewControllerRepresentable Struct 中创建 UIBarButtonItem?【英文标题】:How to create a UIBarButtonItem in UIViewControllerRepresentable Struct? 【发布时间】:2021-07-31 08:20:59 【问题描述】:

我正在尝试在我的自定义导航栏中添加一个按钮,问题是当我向导航栏添加栏按钮项时,它不允许我创建 @objc 方法,为了避免它我创建了一个不同的类对于 objc 方法,但这似乎也不起作用,代码接受选择器方法,但在运行时点击时不会触发。

计划是在点击此按钮时启动一个侧边菜单,该按钮已添加并可见,但该操作不起作用,任何解决方法或帮助将不胜感激。

struct CustomNavigationBar: UIViewControllerRepresentable 
func makeCoordinator() -> Coordinator 
    return CustomNavigationBar.Coordinator(parent: self)


// Properties
/// Ease of Use
var view : AnyView
var title : String  //Will be location of the user

/// On Search and On Cancel Closures
var onSearch : (String) -> ()
var onCancel : () -> ()
var didTapMenu : () -> ()

// Require Closures on initialization
init(view: AnyView, title : String, onSearch : @escaping (String)->(), onCancel : @escaping () -> (), didTapMenu : @escaping () -> ()) 
    self.view = view
    self.title = title
    self.onSearch = onSearch
    self.onCancel = onCancel
    self.didTapMenu = didTapMenu


// Integrating UIKit Navigation Controller with SwiftUI View
func makeUIViewController(context: Context) -> UINavigationController 
    
    //Requires SwiftUIView
    let childView = UIHostingController(rootView: view)
    let controller = UINavigationController(rootViewController: childView)
    
    // Navigation Bar Data
    controller.navigationBar.topItem?.title = title
    controller.navigationBar.prefersLargeTitles = false
    // Navigation Bar Customization
    controller.navigationBar.barTintColor = UIColor(Color.grubSoulRed)
    controller.navigationBar.tintColor = UIColor.white
    controller.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white, .font: UIFont(name: "Poppins-Bold", size: 17)!]
    
    //Adding Navigation Button // ===> Problem Here
    let menuButton = UIBarButtonItem(image: UIImage(named: "hamburgerMenu"), style: .plain, target: nil, action: #selector(navigationActions.hamburgerTapped(_:))) 
    childView.navigationItem.leftBarButtonItem = menuButton

    
    
    // Search Bar
    let searchController = UISearchController()
    searchController.searchBar.placeholder = "Search here for products..."
    /// Search Bar Customization
    searchController.searchBar.searchTextField.backgroundColor = UIColor.white
    searchController.searchBar.searchTextField.layer.masksToBounds = true
    searchController.searchBar.searchTextField.layer.cornerRadius = 17
    searchController.obscuresBackgroundDuringPresentation = true
    /// Setting search bar delegate
    searchController.searchBar.delegate = context.coordinator
    /// Adding Search Bar
    controller.navigationBar.topItem?.searchController = searchController
    controller.navigationBar.topItem?.hidesSearchBarWhenScrolling = false
    
    return controller


func updateUIViewController(_ uiViewController: UINavigationController, context: Context) 



// Search Bar delegate
class Coordinator: NSObject, UISearchBarDelegate 
    
    
    var parent : CustomNavigationBar
    
    init(parent: CustomNavigationBar) 
        self.parent = parent
    
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) 
        self.parent.onSearch(searchText)
    
    
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) 
        self.parent.onCancel()
    


open class navigationActions 
@objc func hamburgerTapped(_ sender : UIButton) 
    print("hamburger tapped")
    // Launch Hamburger menu

【问题讨论】:

【参考方案1】:

无需创建新类, 在Coordinator 类中声明hamburgerTapped 函数并将目标设置为Coordinator 类并从Coordinator 访问

let menuButton = UIBarButtonItem(image: UIImage(named: "hamburgerMenu"), style: .plain, target: context.coordinator, action: #selector(context.coordinator.hamburgerTapped(_:))) // <--- Here!!

Coordinator 类中

// Search Bar delegate
class Coordinator: NSObject, UISearchBarDelegate 
    // Other code
    @objc func hamburgerTapped(_ sender : UIButton)  // <-- Here!!
        print("hamburger tapped")
        // Launch Hamburger menu
    

【讨论】:

很高兴为您提供帮助。你可以接受答案

以上是关于如何在 UIViewControllerRepresentable Struct 中创建 UIBarButtonItem?的主要内容,如果未能解决你的问题,请参考以下文章

如何在表单提交后保留文本(如何在提交后不删除自身?)

如何在异步任务中调用意图?或者如何在 onPostExecute 中开始新的活动?

在 Avkit 中如何使用这三行代码,以及如何将音乐静音”

如何在 JDBC 中启动事务?

如何在 Fragment 中调用 OnActivityResult 以及它是如何工作的?

如何使用 Firebase 在 Web 上托管 Flutter?它的效果如何?