【问题标题】:Scroll view and keyboard issue Swift滚动视图和键盘问题 Swift
【发布时间】:2017-06-15 09:58:54
【问题描述】:

我有一个内部带有滚动视图的视图控制器,我在情节提要中以这种方式(使用自动布局)设置它:

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

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

【问题讨论】:

标签: ios swift xcode uiscrollview uitextfield


【解决方案1】:

试试下面的代码。 首先添加var activeField: UITextField? 以检查文本字段的可用性。 然后在视图中确实 load 调用了这个函数:

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

【讨论】:

    【解决方案2】:

    很可能您用来了解是否存在重叠的读数不正确。
    这是一个常见问题,您在主视图中面对它们,而不是您应该获取文本字段的框架,进行正确的转换以查看键盘是否与其重叠并相应地调整滚动视图的内容插图。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 框架是视图相对于其父视图的位置,因此如果您的 rect 在另一个视图中,您需要转换其框架以获得与其相关的框架主视图。

    【讨论】:

      猜你喜欢
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多