Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a UIPickerView rise from the bottom of the app after pressing a button?

I'm learning Swift by making an iPhone app. The problem I'm running into involves implementing a UIPickerView. I have a button that should, if pressed, make a UIPickerView ascend from the bottom of the screen to a set length over the Main View. Then when the user selects an item from the UIPickerView, the UIPickerView descends down out of the Main View until it's called again.

A good example of what I'm trying to replicate would be when you're adding an alarm in the Clock app, without the view covering up everything and without the label and without the switches.

I'd also would like to have the items I pick from the PickerView trigger a function, but I think that'll be after I get past this hurdle.

Here's the relevant code I have so far.

@IBOutlet weak var pickerContainer: UIView!
@IBOutlet weak var pickerView: UIPickerView!
let picker_values = ["val 1", "val 2", "val 3", "val 4"];
    override func viewDidLoad() {
    super.viewDidLoad()

}


@IBAction func buttonPressed(sender: AnyObject) {
    print("pressing a die!");

}

Thanks in advance!

like image 597
Afrocious Avatar asked May 29 '16 19:05

Afrocious


People also ask

How do you open picker view on button click in Swift?

Just hide/show your uipickerview . Add the picker as subview of main view in storyboard but keep it hidden. On clicking on that button show it.


1 Answers

Ok you can do this by using a UITextField

Step 01:-

Add a UITextField.Select you board style invisible and set tint color white/clear color.enter image description here

enter image description here

Step 02:-

Then add a UIButton on the top of the UITextField,add same width height and constraints.Here is the stroy board hierarchy,

enter image description here

Step 03:-

Then connect BUTTON and TextField to this class functions :-

Connect button to this one @IBAction func button(sender: AnyObject) , and connect UITextField to @IBAction func textField(sender: UITextField).

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var textFeild: UITextField!
let picker_values = ["val 1", "val 2", "val 3", "val 4"]
var myPicker: UIPickerView! = UIPickerView()

override func viewDidLoad() {
    super.viewDidLoad()
    self.myPicker = UIPickerView(frame: CGRectMake(0, 40, 0, 0))
    self.textFeild.delegate = self
    self.myPicker.delegate = self
    self.myPicker.dataSource = self
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//MARK: - Delegates and data sources

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return picker_values.count
}

//MARK: Delegates

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return picker_values[row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    //Your Function
    print("Hello")
}

@IBAction func button(sender: AnyObject) {
    self.textFeild.becomeFirstResponder()
}

func cancelPicker(sender:UIButton) {
    //Remove view when select cancel
    self.textFeild.resignFirstResponder() // To resign the inputView on clicking done.
}

@IBAction func textField(sender: UITextField) {
    //Create the view
    let tintColor: UIColor = UIColor(red: 101.0/255.0, green: 98.0/255.0, blue: 164.0/255.0, alpha: 1.0)
    let inputView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, 240))
    myPicker.tintColor = tintColor
    myPicker.center.x = inputView.center.x
    inputView.addSubview(myPicker) // add date picker to UIView
    let doneButton = UIButton(frame: CGRectMake(100/2, 0, 100, 50))
    doneButton.setTitle("Done", forState: UIControlState.Normal)
    doneButton.setTitle("Done", forState: UIControlState.Highlighted)
    doneButton.setTitleColor(tintColor, forState: UIControlState.Normal)
    doneButton.setTitleColor(tintColor, forState: UIControlState.Highlighted)
    inputView.addSubview(doneButton) // add Button to UIView
    doneButton.addTarget(self, action: "doneButton:", forControlEvents: UIControlEvents.TouchUpInside) // set button click event

    let cancelButton = UIButton(frame: CGRectMake((self.view.frame.size.width - 3*(100/2)), 0, 100, 50))
    cancelButton.setTitle("Cancel", forState: UIControlState.Normal)
    cancelButton.setTitle("Cancel", forState: UIControlState.Highlighted)
    cancelButton.setTitleColor(tintColor, forState: UIControlState.Normal)
    cancelButton.setTitleColor(tintColor, forState: UIControlState.Highlighted)
    inputView.addSubview(cancelButton) // add Button to UIView
    cancelButton.addTarget(self, action: "cancelPicker:", forControlEvents: UIControlEvents.TouchUpInside) // set button click event
    sender.inputView = inputView
}

}

By the way make sure that you UITextField sent event is "Edit Did Begin" Like this :-

enter image description here

Hope that this will gonna help you...

like image 75
Chathuranga Silva Avatar answered Nov 13 '22 20:11

Chathuranga Silva