IOS Swift初学者

在屏幕上,view-scrollview-contentview,contentview中有很多文本框,所以我使用scrollview在一个屏幕上。现在的问题是我无法点击或键入文本字段,因为我知道滚动视图涵盖了contentview。 但是我想input文本框,并能够滚动屏幕。 我试图在这里看到很多答案,但无法找出正确的解决scheme。 scrollview和contentview都是启用用户交互,在滚动视图中打开/closures“延迟内容触摸”/“可取消内容触摸”,但不起作用。 感谢任何帮助。

只需在viewDidLoad上创build两个通知(一个是键盘出现时,另一个是消失时)。

//Notifications for keyBoard when appears. NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow), name:UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillHide), name:UIKeyboardWillHideNotification, object: nil) 

然后调用计算填充的函数并进行滚动。

 func keyboardWillShow(notification:NSNotification){ var userInfo = notification.userInfo! var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue() keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil) var contentInset:UIEdgeInsets = self.scrollview.contentInset contentInset.bottom = keyboardFrame.size.height self.scrollview.contentInset = contentInset } func keyboardWillHide(notification:NSNotification){ var contentInset:UIEdgeInsets = UIEdgeInsetsZero self.scrollview.contentInset = contentInset } 

点击文本框时使用TPKeyboardAvoiding自动pipe理滚动。

https://github.com/michaeltyson/TPKeyboardAvoiding

使TPKeyboardAvoidingScrollView对象,并将文本字段添加到此滚动视图,所以它会正常工作。

在故事板中,在身份检查器中使用类名“TPKeyboardAvoidingScrollView”,并将其绑定到正常工作。