2

I have a UIBarButtonItem:

let addProdButton = UIBarButtonItem(title: "+", style: UIBarButtonItemStyle.plain, target: self, action: #selector(AddProduct))

And the action it is connected to:

@objc func AddProduct()
    {
        performSegue(withIdentifier: "segue_to_add_product", sender: self)
    }

I am adding it to my navigationBar:

addProdButton.isEnabled = true
        self.navigationItem.leftBarButtonItem = addProdButton

And it shows and is clickable when I launch the app:

enter image description here

When I click it, it acts as clickable, but for some reason it doesn't fire up the action I created it with (Breakpoint isn't hit , segue isn't fired up). How do I make it work ?

More questions:

  1. How can I add a border to my UITabBarButton ?
  2. Is there a generic "Add" button in swift that I can use instead? (Or just the stye/picture)
  3. How can I enlarge the text inside the button ?

Edit:

I just noticed the button is clickable ONCE, and then the click animation no longer shows.

My viewDidLoad function:

override func viewDidLoad()
    {
        super.viewDidLoad()
        SetActivityIndicator()
        SetSearchBar()

        addProdButton.isEnabled = true
        self.navigationItem.leftBarButtonItem = addProdButton

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: 200, height: 250)

        self.ProductsCollection = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)

        ProductsCollection.dataSource = self
        ProductsCollection.delegate = self
        ProductsCollection.register(ProductsCollectionViewCell.self, forCellWithReuseIdentifier: "product_collection_cell")
        ProductsCollection.backgroundColor = UIColor.clear

        let topConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 20)
        let bottomConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: -50) //leaving space for search field
        let leadingConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0)
        let trailingConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0)

        ProductsCollection.translatesAutoresizingMaskIntoConstraints = false

        self.view.addSubview(ProductsCollection)
        self.view.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])


    }

my SetSearchBar funciton:

internal func SetSearchBar()
    {
        self.productsSearchController.searchResultsUpdater = self
        self.productsSearchController.delegate = self
        self.productsSearchController.searchBar.delegate = self

        self.productsSearchController.hidesNavigationBarDuringPresentation = false
        self.productsSearchController.dimsBackgroundDuringPresentation = true
        self.productsSearchController.obscuresBackgroundDuringPresentation = false
        productsSearchController.searchBar.placeholder = "Search for tools and resources"
        productsSearchController.searchBar.sizeToFit()

        productsSearchController.searchBar.becomeFirstResponder()

        self.navigationItem.titleView = productsSearchController.searchBar


    }
Ofri
  • 289
  • 5
  • 17

1 Answers1

1

The code you provided should be working so you need to provide more context (show more of your view controller class where this is implemented).

UIKit provides a set of standard UIBarButtonItems called system items. These happen to include an 'add' item, which is displayed as a plus-sign and used throughout iOS. Using this item solves question 2 and 3.

UIBarButtonItem has a convenience constructor for initialising a system item. See the working example below:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let addProdButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addProduct))
        navigationItem.leftBarButtonItem = addProdButton
    }

    @objc func addProduct() {
        print("addProduct() called")
    }
}
l_priebe
  • 774
  • 4
  • 17
  • First of all, thanks for the info about the add button. I am using it now. As for seeing more code - which part do you want to see ? – Ofri Mar 05 '18 at 16:53
  • 1
    Cool! You are not showing where you instantiate your UIBarButtonItem. Assuming it is in class-scope this may be what causes the issue! Try moving the instantiation of the UIBarButtonItem to your viewDidLoad method. – l_priebe Mar 05 '18 at 17:09
  • True, instantiation is this : let addProdButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(AddProduct)) Moving it to "ViewDidLoad" now – Ofri Mar 05 '18 at 17:11
  • Just one more question - is there a way to make the Add button more visible? I want it to draw more attention :) atm it is just a plain "+" sign – Ofri Mar 05 '18 at 17:16
  • Great to hear! It depends on what you want to achieve by making it more visible. If your main concern is that your users will overlook it you should remember that it is the default way of displaying an add button on iOS, and your users will be very familiar with it. If your main concern is that you want to stylize the app more you could provide your own image asset for the UIBarButtonItem (e.g. a fatter plus) or like you suggested; draw a border to make is appear more like a classic button (although I wouldn’t recommend it). – l_priebe Mar 05 '18 at 17:37
  • UIBarButtonItem is more difficult to customise compared to a UIView subclass, and so the easiest way to draw a border is probably by providing a background image to the UIBarButtonItem. – l_priebe Mar 05 '18 at 17:37
  • I just want something that will make sure it won't be overlooked. Atm it's a plain "+" with nothing yo indicate it's a button – Ofri Mar 05 '18 at 18:52
  • Any chance you can give me a hand with another problem I'm having ? :) https://stackoverflow.com/questions/49137461/nserror-domain-com-google-httpstatus-code-404 – Ofri Mar 07 '18 at 08:59