Blättern Sie UITextView, wenn die Tastatur erscheint in swift
Das problem, das ich bin vor ist jetzt, dass es ein UITextField in UIScrollView.
Wenn ich Klicks das UITextField, zeige ich UIPickerView mit UIToolBar. Ich brauche zu bewegen UITextField zu, wenn PickerView präsentiert.
Hier ist mein design
Hier ist mein code
func keyboardWillShow(notification: NSNotification)
{
let userInfo = notification.userInfo
if let info = userInfo
{
let animationDurationObject =
info[UIKeyboardAnimationDurationUserInfoKey] as NSValue
let keyboardEndRectObject =
info[UIKeyboardFrameEndUserInfoKey] as NSValue
var animationDuration = 0.0
var keyboardEndRect = CGRectZero
animationDurationObject.getValue(&animationDuration)
keyboardEndRectObject.getValue(&keyboardEndRect)
let window = UIApplication.sharedApplication().keyWindow
keyboardEndRect = view.convertRect(keyboardEndRect, fromView: window)
let intersectionOfKeyboardRectAndWindowRect =
CGRectIntersection(view.frame, keyboardEndRect);
UIView.animateWithDuration(animationDuration, animations: {[weak self] in
self!.scrollView.contentInset = UIEdgeInsets(top: 0,
left: 0,
bottom: intersectionOfKeyboardRectAndWindowRect.size.height,
right: 0)
self?.scrollView.scrollRectToVisible(self!.activeTextField.frame, animated: true)
})
}
}
func keyboardWillHide(notification: NSNotification)
{
let userInfo = notification.userInfo
if let info = userInfo{
let animationDurationObject =
info[UIKeyboardAnimationDurationUserInfoKey]
as NSValue
var animationDuration = 0.0;
animationDurationObject.getValue(&animationDuration)
UIView.animateWithDuration(animationDuration, animations: {
[weak self] in
self?.scrollView.contentInset = UIEdgeInsetsZero
self?.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero
})
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField.resignFirstResponder()
return true
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool
{
var returnVal = true
self.activeTextField = textField
if textField == self.pickerTextField
{
returnVal = false
var yval:CGFloat = self.view.bounds.size.height - 206
self.pickerContainerView.frame = CGRectMake(0, yval, self.view.bounds.size.width, self.pickerContainerView.frame.size.height)
self.pickerContainerView.hidden = false
self.view.addSubview(self.pickerContainerView)
}
return returnVal
}
func textFieldDidEndEditing(textField: UITextField) {
self.activeTextField = nil
textField.resignFirstResponder()
}
Wenn ich auf anderen Feldern, denen TextField geht, wenn die Tastatur präsentiert. Aber was ist zu tun bei PickerTextField
Ideen für UItextView zu
Dank.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Swift 3 Version: