iOS Swift初学者 [英] IOS Swift beginner

查看:85
本文介绍了iOS Swift初学者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a screen, view-scrollview-contentview, there are a lot of textfields in the contentview so I use a scrollview to make them in one screen.Now the problem is I could not click or type in the textfield because I know the scrollview has covered the contentview. But I want to type in the textfield and be able to scroll the screen as well. I tried to see a lot of answers here but could not figure out the correct solution. Both the scrollview and contentview are user interaction enabled, switch on/off the "delays content touches"/"Cancellable content touches" in scrollview but doesn't work. Appreciate any help.

推荐答案

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

Just create two notifications (once when the keyboard appears and other when it dissapear) on the 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)

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

then call the function which calculates the padding and make an scroll.

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
}

这篇关于iOS Swift初学者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆