IOS - Swift - 向 BarButtonItem 的自定义 View 添加目标和操作

标签 ios iphone uinavigationbar uinavigationitem

我正在创建一个自定义 View ,将 uiimageview 作为 subview 。然后我在我的导航栏中使用该自定义 View 作为 rightBarButtonItem。这可以在正确的位置显示正确的图标,但无论出于何种原因,我在“操作”中定义的函数都不会被调用,因此当我点击 rightBarButtonItem 时不会执行 segue。奇怪的是,如果我不插入自定义 View ,而是注释掉那个位,然后只为 rightItem 设置标题、目标和操作,那么该功能就会正确执行。以某种方式添加自定义 View 会混淆目标和操作属性,但我似乎无法弄清楚如何修复它。任何帮助将不胜感激!

//create custom view for right item and set it
    var imageViewRight:UIImageView = UIImageView()
    imageViewRight.frame = CGRectMake(5, 10, 35, 25)
    let rightImage:UIImage = UIImage(named: "cameraIconInactive")!
    imageViewRight.image = rightImage
    var rightView:UIView = UIView()
    rightView.frame = CGRectMake(0, 0, 45, 45)
    rightView.addSubview(imageViewRight)
    var rightItem:UIBarButtonItem = UIBarButtonItem()
   //this line right below is the problem - if I comment it out and replace with a simple rightItem.title = "test" and leave everything else the same, then the method runs properly
    rightItem.customView = rightView
    rightItem.target = self
    rightItem.action = "pushProfileToCamera"
    self.navigationItem.rightBarButtonItem = rightItem

}

func pushProfileToCamera(){
    println("pushing profile to camera")
    self.performSegueWithIdentifier("pushProfileToCamera", sender: nil)
}

编辑:

在我看到这些答案建议之前,实际上已经坐了 24 小时并提出了这个解决方案。我有什么理由不应该这样做?它有效..

//create the custom rightBarButtonItem
    //create the imageView with icon
    var imageViewRight:UIImageView = UIImageView()
    imageViewRight.frame = CGRectMake(5, 10, 35, 25)
    var rightImage:UIImage = UIImage(named: "cameraIconInactive")!
    imageViewRight.image = rightImage
    //put the imageView inside a uiview
    var rightView:UIView = UIView()
    rightView.frame = CGRectMake(0, 0, 45, 45)
    rightView.addSubview(imageViewRight)
    //create the tap gesture recognizer for that uiview
    var rightGestureRecognizer:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "pushProfileToCamera")
    rightView.addGestureRecognizer(rightGestureRecognizer)
    //create the uibarbuttonitem, assign our custom view to it, and insert it in the nav bar!
    var rightItem:UIBarButtonItem = UIBarButtonItem()
    rightItem.customView = rightView
    self.navigationItem.rightBarButtonItem = rightItem

最佳答案

您可以尝试将 UIButton 添加到 rightBarButtonItem,而不是链接 rightItemAction 并将 UIView 添加到 customView:

    var button: UIButton = UIButton()
    button.setImage(UIImage(named: "cameraIconInactive"), forState: .Normal)
    button.frame = CGRectMake(0, 0, 45, 45)
    button.targetForAction("pushProfileToCamera", withSender: nil)

    var rightItem:UIBarButtonItem = UIBarButtonItem()
    rightItem.customView = button
    self.navigationItem.rightBarButtonItem = rightItem

关于IOS - Swift - 向 BarButtonItem 的自定义 View 添加目标和操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26774690/

相关文章:

ios - NSAttributedString 到 NSString UTF-8

iOS:AVAudioRecorder.prepareToRecord() 失败但没有错误

swiftui - SwiftUI 的 NavigationLink DetailView 纵向和横向的导航栏高度

ios - 导航栏大标题不缩进

ios - UITableview 分隔符不为 iOS9 隐藏

ios - swift 从不同的 Storyboard 中移动另一个 ViewController

ios - 需要帮助弄清楚为什么我的 UIImageView 不会缩放/平移 - 包含代码片段

iphone - 能够在 iPhone 上构建应用程序,但缺少应用程序的核心数据

iphone - Xcode 3.2.4 的配置文件如何?

NavigationBar 中的 iOS 11 Logo 和标题