滚动视图和键盘问题Swift

时间:2017-01-30 09:34:41

标签: ios swift xcode uiscrollview uitextfield

我有一个带有scrollview的视图控制器,我在故事板中以这种方式设置它(带有autolayout):

Example

如您所见,我在滚动视图中添加了最后一个视图中的所有对象(称为“viewsotto”)。 我的问题是: 这些对象中的一些是文本字段,我希望当我点击它并且键盘显示它可以在文本字段中时,以便我可以看到我在其中写的内容。 出于这个原因,我这样做:

NotificationCenter.default.addObserver(self, selector: #selector(userProfiloGiusto.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(userProfiloGiusto.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

func keyboardWillShow(notification: NSNotification) {

        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            if self.view.frame.origin.y == 0{
                self.view.frame.origin.y -= keyboardSize.height
            }
        }

    }

    func keyboardWillHide(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            if self.view.frame.origin.y != 0{
                self.view.frame.origin.y += keyboardSize.height
            }
        }
    }

但它不起作用。我做错了什么?

2 个答案:

答案 0 :(得分:0)

尝试以下代码。 首先添加var activeField: UITextField?以检查文本字段的可用性。 然后在视图中加载调用此函数:

override func viewDidLoad() {
    super.viewDidLoad()self.registerForKeyboardNotifications()

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
    view.addGestureRecognizer(tap)
    tap.cancelsTouchesInView = false}


func registerForKeyboardNotifications()
{
    //Adding notifies on keyboard appearing
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ProfileReviewViewController.keyboardWasShown(_:)), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ProfileReviewViewController.keyboardWillBeHidden(_:)), name: UIKeyboardWillHideNotification, object: nil)
}


func deregisterFromKeyboardNotifications()
{
    //Removing notifies on keyboard appearing
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWasShown(notification: NSNotification)
{
    //Need to calculate keyboard exact size due to Apple suggestions
    let info : NSDictionary = notification.userInfo!
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)


    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
    var aRect : CGRect = self.view.frame
    aRect.size.height -= keyboardSize!.height
    if activeField != nil
    {
        if (CGRectContainsPoint(aRect, activeField!.frame.origin))
        {
            scrollView.scrollRectToVisible(activeField!.frame, animated: true)
        }
    }
}

func keyboardWillBeHidden(notification: NSNotification)
{
    self.scrollView.contentInset = UIEdgeInsetsZero
}
//MARK : Textfield delegate methods
func textFieldDidBeginEditing(textField: UITextField) {
    activeField = textField
}

func textFieldShouldReturn(textField: UITextField) -> Bool {

    self.view.endEditing(true)
    activeField = nil
    return false
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {  //delegate method

    activeField = nil
    return true
}
func dismissKeyboard() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.

    activeField = nil
    view.endEditing(true)
}

答案 1 :(得分:0)

最有可能的是,如果存在重叠,您用来理解的读数是不正确的  这是一个常见的问题,你正在用你的主视图面对它们,而你应该得到文本框的框架,进行正确的转换以查看键盘是否重叠并相应地调整滚动视图的内容插入.z
Managing the keyboard文档中对此进行了详细解释 键盘显示时:

func keyboardWasShown(notification: NSNotification)
{
    let info : NSDictionary = notification.userInfo!
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size!
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)


    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    var aRect : CGRect = self.view.frame
    aRect.size.height -= keyboardSize.height
    if let actField = activeField
    {
        if (CGRectContainsPoint(aRect, actField.frame.origin))
        {
            scrollView.scrollRectToVisible(actField.frame, animated: true)
        }
    }
}

重要的一点是,当他们询问视图高度时,他们会减去键盘高度,并检查文本视图是否在该区域内。
这个计算可能会随着你的视图层次结构发生变化,这是因为ea frame是一个视图的位置,它尊重它的superview,因此如果你的rect在另一个视图中,你需要转换它的帧来获取它的相关框架。主要观点。