0

I am new to XCode and Swift.

I am building an App with a dataPickerView so that user to set date easily. The problem now is the code from "another question I ask" works fine in iPhone (see the image below), but when i test it in iPad, it become bad.

So how can I fix the code and make it a better look?

Here is the code:

    @IBAction func dateTextInputPressed(sender: UITextField) {

        //Create the view
        let inputView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, 240))


        var datePickerView  : UIDatePicker = UIDatePicker(frame: CGRectMake(0, 40, 0, 0))
        datePickerView.datePickerMode = UIDatePickerMode.Date
        inputView.addSubview(datePickerView) // add date picker to UIView

        //let doneButton = UIButton(frame: CGRectMake(110, 0, 100, 50))
        let doneButton = UIButton(frame: CGRectMake((self.view.frame.size.width/2) - (100/2), 0, 100, 50))
        doneButton.setTitle("done", forState: UIControlState.Normal)
        doneButton.setTitle("done", forState: UIControlState.Highlighted)
        doneButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        doneButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)

        inputView.addSubview(doneButton) // add Button to UIView

        doneButton.addTarget(self, action: "doneButton:", forControlEvents: UIControlEvents.TouchUpInside) // set button click event

        sender.inputView = inputView
        datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)

        handleDatePicker(datePickerView) // Set the date on start.
    }

Thanks!

PS: Sorry for my bad English. :(

dataPickerView works great in iPhone datePickerView works not such good in iPad

Community
  • 1
  • 1
He Yifei 何一非
  • 2,592
  • 4
  • 38
  • 69
  • 2
    the `UIPopoverController` is your unsung hero on iPad. – holex Jan 23 '15 at 16:22
  • @holex sorry is there any example for it? – He Yifei 何一非 Jan 23 '15 at 23:41
  • there are hundreds on the net, you just need to Google it. :) – holex Jan 24 '15 at 08:02
  • @holex but i have no idea how to let this date picker both use in iPhone & iPad via `UIPopoverController` – He Yifei 何一非 Jan 24 '15 at 08:07
  • you cannot use `UIPopoverController` in iPhone, it is not available (the app will crash if you'd try), so you need to detect which type of device you are on in runtime, and you need to show the standard date picker on iPhone and show it inside a popover on iPad. – holex Jan 24 '15 at 08:15
  • @holex sorry but I really cannot find such code and every code i find in internet go crash. Also, I still have no idea about get datePicker's value which is in another view outside options page and fill it into the text input. Can you help me please? – He Yifei 何一非 Jan 24 '15 at 09:51
  • the `UIPopoverController` is not a view, and its content is not outside of your option's page... but please first make some effort to work out a solution with `UIPopoverController` and when you feel to be stuck anywhere, just raise a question. I'm not sure that you would find anyone who will do this essential job for you for free – but you can learn from tutorials or from Apple's guidance. :) – holex Jan 24 '15 at 10:10

0 Answers0