【问题标题】:How to add action to UINavigationItem?如何向 UINavigationItem 添加操作?
【发布时间】:2017-02-02 12:14:28
【问题描述】:

我需要动态创建Navigation Bar并在左侧设置Cancel按钮。

有代码我如何尝试这样做

var navBar: UINavigationBar = UINavigationBar()
func setNavBarToTheView() {
    navBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 80.0)
    navBar.backgroundColor = (UIColor.black)

    let navTitle = UINavigationItem(title: "Camera")
    let navCancel = UINavigationItem(title: "Cancel")

    navBar.setItems([navCancel, navTitle], animated: true)
    view.addSubview(navBar)
}

但是我怎样才能将动作监听器添加到Cancel 按钮??

【问题讨论】:

    标签: swift xcode navigation navigationbar


    【解决方案1】:

    UINavigationItem 是一个模型对象,用于存储有关屏幕按钮的信息。

    您需要的是一个 UINavigationItemleftBarButtonItemrightBarButtonItem 集。

    let item = UINavigationItem(title: "Title")
    item.leftBarButtonItem = UIBarButtonItem(title:"Cancel",
                                             style:.plain,
                                             target:self,
                                             action:#selector(cancelTapped))
    
    navBar.setItems([item], animated: true)
    

    【讨论】:

      猜你喜欢
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 2021-10-29
      相关资源
      最近更新 更多