0

I have a problem with UITextField action. When I tap on it it is supposed to pop up a date picker view. The date picker piew popped up but I can't roll it or change the date. It seems like it is stuck. her is an image of my result the date Picker is freezes in November 16 2019

@IBAction func textFieldDidBegin(_ sender: Any) 
{
    let pickerView: UIDatePicker = {
        let view = UIDatePicker()
        view.layer.shadowColor = UIColor.rgb(5, 0, 0, 0.13).cgColor
        view.layer.shadowRadius = 14
        view.layer.shadowOffset = CGSize(width: 0, height: 9)
        view.layer.shadowOpacity = 1
        view.backgroundColor = .white

        return view
    }()

    pickerView.timeZone = .current
    pickerView.datePickerMode = UIDatePicker.Mode.date

    view.addSubview(pickerView)

    pickerView.centerInSuperview()
}


extension UIColor {

    class func rgb(_ red: CGFloat, _ green: CGFloat, _ blue: CGFloat, _ alpha: CGFloat = 1) -> UIColor {
        return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: alpha)
    }

}
extension UIView {

    func centerInSuperview() {
       self.translatesAutoresizingMaskIntoConstraints = false
        self.centerHorizontallyInSuperview()
        self.centerVerticallyInSuperview()
    }

    func centerHorizontallyInSuperview(){
        let c: NSLayoutConstraint = NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: self.superview, attribute: .centerX, multiplier: 1, constant: 0)
        self.superview?.addConstraint(c)
    }

    func centerVerticallyInSuperview(){
        let c: NSLayoutConstraint = NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: self.superview, attribute: .centerY, multiplier: 1, constant: 0)
        self.superview?.addConstraint(c)
    }
}

Also when I run the code from textFieldDidBegin into ViewDidLoad() function it's works fine .

yousef
  • 1
  • 2
  • https://www.youtube.com/watch?v=aa-lNWUVY7g What about this tutorial? – J A S K I E R Nov 16 '19 at 18:51
  • thank you But it doesn't work for me I want when a tap into text field the pop up custom date picker appears @Oleksandr – yousef Nov 16 '19 at 18:57
  • What about this? Is it similar to your problem? https://stackoverflow.com/questions/24552117/swift-using-uidatepicker-as-uitextfield-inputview-not-updating/24552272 – J A S K I E R Nov 16 '19 at 18:59
  • Could you show, please the whole code? I don't see the "textFieldDidChange" function... or any similar. Show more code, including the names of functions you call? – J A S K I E R Nov 16 '19 at 19:04
  • @Oleksandr I have edited my Question with whole code and the result – yousef Nov 16 '19 at 19:16
  • First of all, make sure your `@IBAction` is attached to the right event. I copied your code, and it works, of course, it also displays a keyboard, but this is separate thing. – kodelit Nov 17 '19 at 15:04
  • @kodelit is attached to the right event ? explain please – yousef Nov 17 '19 at 15:59
  • I mean if you use @IBAction you probably linked it to one of the UITextField events in your storyboard, events like: Touch Up Inside, Editing Did Begin, etc. Or you are invoking your method `textFieldDidBegin(_:)` somewhere in the code, but didn't show us where and how. – kodelit Nov 17 '19 at 17:31
  • @kodelit Yes i link it to Editing Did Begin Event in the storyboard the date picker view popes up but it seams like it is freezed no selection inside,Although when i put the same code in sample button with Touch up inside event it works fine can you explain why this happen – yousef Nov 17 '19 at 19:06
  • ur code works fine on my iPhone 11 simulator, did u tried to run it on your iPhone device ? – youssef mostafa Nov 17 '19 at 21:55
  • also, try to add the subView and constraints in viewWillAppear. – youssef mostafa Nov 17 '19 at 21:57
  • For sure `translatesAutoresizingMaskIntoConstraints = false` can't be in `centerInSuperview()` because it has to be set before you add the view as a subview. Now there already are constraints responsible for autoresizing and may be conflicting with other constraints. It does not have to be the cause of your issue, but you need to eliminate such errors before you start looking forward. – kodelit Nov 18 '19 at 10:22

1 Answers1

0

I have no idea guys but this is solved by restarting my os , if anyone knows why please i want to understand thank you

yousef
  • 1
  • 2