Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - pop up view, date picker?

Tags:

xcode

ios

swift

I've googled for hours and have tried a handful of tutorials, but haven't been able to get this working:

I have a TableView, and I want to make it so pressing on a cell presents a popup that has a date picker.

I have my custom viewcontroller with the date picker presenting (popping up from the bottom), but it takes up the entire screen. Thoughts? I found one mention of this exact issue while googling but the solution didn't work.

like image 204
Max Avatar asked Dec 04 '25 12:12

Max


1 Answers

One possibility is to overlay a subview (object of UIView) (with a date picker and a done button) on top of your tableview. Then use .hidden feature of the subview to hide/show the view. The following is an example of the tableviewcontroller. When setting up the storyboard make sure that the subview has the layout constraints so the date picker is positioned properly. I used the "resolve auto layout issues" and it worked good. Unless you do special processing the subview will get positioned at the bottom of the rows. If you have a lot of rows the aubview will get clipped or hidden completely. So it is better to position the subview at the relative to the bottom of the page in your auto layout.

Here is a simple example that worked well for me. In viewDidLoad the subview is hidden. When you click on any row it will show the subview and the date picker. When you press done it will hide the subview again.

import UIKit

class TableViewController: UITableViewController {

    @IBAction func doneButton(sender: UIButton) {
        // process the date using datePickerOutlet properties
        subView.hidden = true // hide the subview and its components
    }
    @IBOutlet weak var subView: UIView!
    @IBOutlet weak var datePickerOutlet: UIDatePicker!
    @IBAction func datePicker(sender: UIDatePicker) {
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        subView.hidden = true // hide the subview and its components
    }

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

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("\(indexPath.row)")
        subView.hidden = false // show the subview and its components
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel!.text = "\(indexPath.row)"
        return cell
    }
}
like image 138
Syed Tariq Avatar answered Dec 06 '25 05:12

Syed Tariq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!