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:
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:
- How can I add a border to my UITabBarButton ?
- Is there a generic "Add" button in swift that I can use instead? (Or just the stye/picture)
- 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
}