IOS - Swift - add target and actions to BarButtonItem customView
I am creating a custom view with uiimageview as subview. Then I use this custom view in my navbar as rightBarButtonItem. This works to display the correct icon in the right place, but for some reason the function I define in the "action" is never called, so the segue fails when I click on the rightBarButtonItem. Oddly enough, if I DO NOT insert the custom view, but instead comment out that bit and just set the title, target and action to rightItem, then the function is executed correctly. adding somehow that custom views messes up with target and action properties, but I can't figure out how to fix this. 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 seeing these answers. Why don't I do this? He 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
source to share
You can try instead of linking rightItemAction and adding UIView to customView, add UIButton to 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
source to share
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)
source to share