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

5

6

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
Coca answered 6/11, 2014 at 8:18 Comment(0)
A
7

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
Atli answered 6/11, 2014 at 8:38 Comment(0)
I
4

Try like this

var rightItem:UIBarButtonItem = UIBarButtonItem(image: rightImage, landscapeImagePhone: rightImage, style: UIBarButtonItemStyle.Plain, target: self, action: "pushProfileToCamera")
Intromission answered 6/11, 2014 at 8:51 Comment(0)
N
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)
Nubbly answered 1/2, 2016 at 7:13 Comment(0)
W
0

UIBarButtonItem already has this initializer init(image: UIImage?, style: UIBarButtonItemStyle, target: AnyObject?, action: Selector)

Windbreak answered 24/4, 2015 at 8:37 Comment(0)
B
0

Easy and shortest way to do that

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
}
Boer answered 17/2, 2022 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.