IOS Swift 初学者

IOS Swift beginner

在 view-scrollview-contentview 屏幕中,contentview 中有很多文本字段,所以我使用滚动视图将它们合二为一 screen.Now 问题是我无法点击或输入textfield 因为我知道 scrollview 已经覆盖了 contentview。但我想在文本字段中输入内容并能够滚动屏幕。我试图在这里看到很多答案,但找不到正确的解决方案。 滚动视图和内容视图都启用了用户交互,在滚动视图中切换 on/off 和 "delays content touches"/"Cancellable content touches" 但不起作用。 感谢任何帮助。

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

https://github.com/michaeltyson/TPKeyboardAvoiding

创建 TPKeyboardAvoidingScrollView 对象并将文本字段添加到此滚动视图中,这样它就可以正常工作了。

在情节提要中,在身份检查器中使用 class 名称 "TPKeyboardAvoidingScrollView" 并将其绑定以正常工作。

只需在 viewDidLoad 上创建两个通知(一次在键盘出现时,另一个在键盘消失时):

  //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
}