iosiphoneuinavigationbaruinavigationitem

IOS - Swift - adding target and action to BarButtonItem's customView


I'm creating a custom view, with a uiimageview as a subview. I'm then using that custom view in my navigation bar as the rightBarButtonItem. This works to display the proper icon in the proper location, but for whatever reason the function I define in "action" is never being called, so the segue isn't performed when I tap on the rightBarButtonItem. Oddly enough if I do NOT insert a custom view, but instead comment that bit out and just set a title, target, and action for rightItem then the function is performed properly. somehow adding that custom view messes with the target and action properties, but I can't seem to figure out how to fix it. Any help would be greatly appreciated!

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

EDIT:

actually sat on this for 24 hours and came up with this solution before I saw these answer suggestions.. any reason I shouldn't do this? It works..

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

Solution

  • You can try to instead of linking the rightItemAction and adding a UIView to the customView, add a UIButton to the rightBarButtonItem:

        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