IOS Swift初学者

时间:2016-08-09 01:47:57

标签: ios swift uiscrollview swift2

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

2 个答案:

答案 0 :(得分:0)

单击文本字段时,使用TPKeyboardAvoiding进行自动管理滚动。

https://github.com/michaeltyson/TPKeyboardAvoiding

制作TPKeyboardAvoidingScrollView对象并将文本字段添加到此滚动视图中,以便它可以正常工作。

在故事板中使用类名" TPKeyboardAvoidingScrollView"在身份检查员身上并将其绑定以运作良好。

答案 1 :(得分:0)

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
}
相关问题