IOS - Swift - 向 BarButtonItem 的 customView 添加目标和操作

Posted

技术标签:

【中文标题】IOS - Swift - 向 BarButtonItem 的 customView 添加目标和操作【英文标题】:IOS - Swift - adding target and action to BarButtonItem's customView 【发布时间】:2014-11-06 08:18:57 【问题描述】:

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

//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

【问题讨论】:

【参考方案1】:

您可以尝试将 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

【讨论】:

【参考方案2】:

这样试试

var rightItem:UIBarButtonItem = UIBarButtonItem(image: rightImage, landscapeImagePhone: rightImage, style: UIBarButtonItemStyle.Plain, target: self, action: "pushProfileToCamera")

【讨论】:

【参考方案3】:
self.navigationController?.navigationBarHidden =  false

    let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64)) // Offset by 20 pixels vertically to take the status bar into account

    navigationBar.backgroundColor = UIColor.blueColor()
    navigationBar.delegate = self;

    // Create a navigation item with a title
    let navigationItem = UINavigationItem()

    //menu button
    let menubutton: UIButton = UIButton(frame: CGRectMake(0, 0, 30, 30))
    menubutton.setImage(UIImage(named: "menu"), forState: UIControlState.Normal)
    menubutton.addTarget(self, action: "btn_clicked", forControlEvents: UIControlEvents.TouchUpInside)

    //menu button custom view
    let leftView = UIView(frame: CGRectMake(0,0,30,30))
    leftView.addSubview(menubutton)

    //left uibarbutton
    let leftItem:UIBarButtonItem = UIBarButtonItem(customView: leftView)
    navigationItem.leftBarButtonItem = leftItem


    //searchButton
    let searchbutton: UIButton = UIButton()
    searchbutton.setImage(UIImage(named: "search1x"), forState: UIControlState.Normal)
    searchbutton.frame = CGRectMake(0, 0, 30, 30)
    searchbutton.addTarget(self, action: "btn_clicked", forControlEvents: UIControlEvents.TouchUpInside)


    //menu button custom view
    let rightView = UIView(frame: CGRectMake(0,0,30,30))
    rightView.addSubview(searchbutton)

    //right uibarbutton
    let rightItem:UIBarButtonItem = UIBarButtonItem(customView: rightView)
    navigationItem.rightBarButtonItem = rightItem

    // Assign the navigation item to the navigation bar
    navigationBar.items = [navigationItem]

    // Make the navigation bar a subview of the current view controller
    self.view.addSubview(navigationBar)

【讨论】:

【参考方案4】:

UIBarButtonItem 已经有这个初始化器init(image: UIImage?, style: UIBarButtonItemStyle, target: AnyObject?, action: Selector)

【讨论】:

【参考方案5】:

简单快捷的方法

private lazy var profileInfoView: ProfileInfoView = 
   let view = ProfileInfoView()
   let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(didTapProfileButton))
   view.addGestureRecognizer(tapGesture)
   return view
()
override func viewDidLoad() 
   super.viewDidLoad()
   self.navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: profileInfoView)]

@objc private func didTapProfileButton() 
    // do your actions

【讨论】:

以上是关于IOS - Swift - 向 BarButtonItem 的 customView 添加目标和操作的主要内容,如果未能解决你的问题,请参考以下文章

navigationItem.set_BarButton 不起作用 - 迅速

IOS - Swift - 向 BarButtonItem 的 customView 添加目标和操作

使用 Swift 向 iOS 钥匙串添加项目和查询

使用 iOS 7 滑动导航时导航栏出现故障

在 IOS Swift 中定期向用户发送通知

导航栏中的 Barbutton 项目左栏按钮粘在左上角