title: Swift - Programmatically create and display UIPickerView when BarButtonItem is pressed
body: <p>Initially I wanted to add a hidden UIPickerView onto the Main.storyboard alongside my existing UISearchBar and when a BarButtonItem is clicked, the UIPickerView should be displayed; but it appears I cannot have them both at once in a given space.</p>
  
  <p>So instead, my best alternative was to create it programmatically. I've followed existing tutorials (<a href="http://sourcefreeze.com/ios-uipickerview-example-using-swift/" rel="noreferrer">http://sourcefreeze.com/ios-uipickerview-example-using-swift/</a>) and similar questions (<a href="https://stackoverflow.com/questions/24836369/programmatically-create-and-show-uipickerview">Programmatically Create and Show UIPickerView</a>) and seems like I do (?) have a UIPickerView as the description of it is being printed and I get the following:</p>
  
  <pre><code>&lt;UIPickerView: 0x7f86425b1fb0; frame = (100 100; 100 162); layer = &lt;CALayer: 0x7f8642543a20&gt;&gt;
  </code></pre>
  
  <p>Here is part of my current code which may be of help: </p>
  
  <p><strong>AnimalTableViewController.swift</strong></p>
  
  <pre><code>import UIKit
  
  class AnimalTableViewController: UITableViewController, UINavigationControllerDelegate, UISearchBarDelegate, UISearchDisplayDelegate, UISearchResultsUpdating, UIPickerViewDelegate, UIPickerViewDataSource {
  
      @IBOutlet var segmentedSortOption: UISegmentedControl!
      var array : NSArray = Animal.animalStruct.jsonResult["animal"] as NSArray
      var filteredArray = [[String:AnyObject]]()
      var timer = NSTimer()
      var counter:Int = 1
      var typePickerView: UIPickerView = UIPickerView()
  
      @IBOutlet var typeBarButton: UIBarButtonItem!
      var resultSearchController = UISearchController()
  
      var indexArray:String!
  
      @IBAction func refresh(sender: AnyObject) {
  
          self.tableView.reloadData()
          println("refreshed")
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          self.typePickerView.hidden = true
          self.typePickerView.dataSource = self
          self.typePickerView.delegate = self
          self.typePickerView.frame = CGRectMake(100, 100, 100, 162)
          self.typePickerView.backgroundColor = UIColor.blackColor()
          self.typePickerView.layer.borderColor = UIColor.whiteColor().CGColor
          self.typePickerView.layer.borderWidth = 1        
          timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
  
          self.resultSearchController = ({
              let controller = UISearchController(searchResultsController: nil)
              controller.searchResultsUpdater = self
              controller.dimsBackgroundDuringPresentation = false
              controller.searchBar.sizeToFit()
  
              self.tableView.tableHeaderView = controller.searchBar
  
              return controller
          })()
      }
  
      func numberOfComponentsInPickerView(pickerView: UIPickerView) -&gt; Int {
          return 1
      }
  
      func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -&gt; Int {
          return array.count
      }
  
      func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -&gt; String! {
          return array[row]["type1"] as String
      }
  
      func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
          typeBarButton.title = array[row]["type1"] as? String
          typePickerView.hidden = false
      }
  
      func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -&gt; CGFloat {
          return 36.0
      }
  
      func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -&gt; CGFloat {
          return 36.0
      }
  
      @IBAction func typePickerViewSelected(sender: AnyObject) {
          typePickerView.hidden = false
          println(typePickerView.description)
      }
  
  }
  </code></pre>
  
  <p>Please could you help me display the programmatically created UIPickerView when the BarButtonItem is pressed? If you have any more questions, please do ask.</p>
  
  <p>Many thanks.</p>