jobandtalent / AnimatedTextInput

Animated UITextField and UITextView replacement for iOS
MIT License
760 stars 128 forks source link

First resigner bug with .generic #48

Open Alex293 opened 7 years ago

Alex293 commented 7 years ago

It seems that there is a bug with the last version (0.4.0) with the .generic type.

I have two texts inputs : One simple / default which become the first responder in view did load. A second of generic type with a date/time picker and a tool bar (I used the code provided in https://github.com/jobandtalent/AnimatedTextInput/issues/39).

Now if I click on the field, the date the picker is shaun as expected but the field isn't blue as it should. Same thing if I force it to be the first responder It is blue but if I touch the first one, the blue tint does not disappear. This is doesn't append with other provided types.

Here is a small vc code which demonstrate the bug :

class NewEventController : UIViewController { @IBOutlet weak var titleTextInputView: AnimatedTextInput! { didSet { titleTextInputView.placeHolderText = "Title" } }

@IBOutlet weak var dateTextInputView: AnimatedTextInput!
{
    didSet
    {
        // TOOLBAR
        let toolBar = UIToolbar()
        toolBar.barStyle = UIBarStyle.default
        toolBar.isTranslucent = true
        toolBar.tintColor = UIColor.black
        toolBar.sizeToFit()
        let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(NewEventController.handleDoneButton(sender:)))
        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        toolBar.setItems([ spaceButton, doneButton], animated: false)
        toolBar.isUserInteractionEnabled = true

        // INPUTVIEW
        let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        datePicker.datePickerMode = .dateAndTime
        datePicker.backgroundColor = .white
        datePicker.minimumDate = Date().adding(.minute, value: 5).nearestFiveMinutes
        datePicker.minuteInterval = 5
        datePicker.addTarget(self, action: #selector(NewEventController.datePickerValueChanged(sender:)), for: .valueChanged)

        // ASSEMBLY
        let myTextInput = DateTextInput()
        myTextInput.inputView = datePicker
        myTextInput.inputAccessoryView = toolBar
        dateTextInputView.type = .generic(textInput: myTextInput)

        // VALUES
        dateTextInputView.placeHolderText = "Date"
        updateDateField(with: datePicker.date)
    }
}

override func viewDidLoad()
{
    titleTextInputView.becomeFirstResponder()
}

func handleDoneButton(sender: UIButton)
{
    _ = dateTextInputView.resignFirstResponder()
}

func datePickerValueChanged(sender: UIDatePicker)
{
    updateDateField(with: sender.date)
}

func updateDateField(with date : Date)
{
    let formatter = DateFormatter()
    formatter.dateStyle = .long
    formatter.timeStyle = .short
    dateTextInputView.text = formatter.string(from: date)
}

}

class DateTextInput: UITextView, TextInput { var view: UIView { return self }

var currentText: String?
{
    get { return text }
    set { self.text = newValue }
}

var currentSelectedTextRange: UITextRange?
{
    get { return self.selectedTextRange }
    set { self.selectedTextRange = newValue }
}

var textAttributes: [String: Any]?
{
    didSet
    {
        guard let attributes = textAttributes else { return }
        typingAttributes = attributes
    }
}

weak var textInputDelegate: TextInputDelegate?

var currentBeginningOfDocument: UITextPosition?
{
    return self.beginningOfDocument
}

func changeReturnKeyType(with newReturnKeyType: UIReturnKeyType)
{
    returnKeyType = newReturnKeyType
}

func currentPosition(from: UITextPosition, offset: Int) -> UITextPosition?
{
    return position(from: from, offset: offset)
}

}