【问题标题】:Keyboard management blocking UITextView键盘管理阻塞 UITextView
【发布时间】:2016-03-25 14:02:45
【问题描述】:

所以我遵循了关于在键盘上方移动文本的苹果指南,当文本字段位于滚动视图上时,这可以正常工作,但是滚动视图包含一个集合视图,并且该集合视图加载一个包含文本字段的笔尖,它们都被分配为委托,当他们被按下时,委托的 didEdit/endEdit 功能确实会触发,但是键盘管理代码没有按预期工作......这是键盘管理代码

http://creativecoefficient.net/swift/keyboard-management/

这里是正在使用的代码的链接..

func keyboardWillBeShown(sender: NSNotification) {
    print("KEYBOARD SHOWN")

    let info: NSDictionary = sender.userInfo!
    let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as! NSValue
    let keyboardSize: CGSize = value.CGRectValue().size
    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0,keyboardSize.height, 0.0)
    print(keyboardSize.height)
    ScrollView.contentInset = contentInsets
    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
    let activeTextFieldOrigin: CGPoint? = activeTextFieldRect?.origin
    if (!CGRectContainsPoint(aRect, activeTextFieldOrigin!)) {
    let dHeight = displayView.frame.height

    ScrollView.scrollRectToVisible(activeTextFieldRect!, animated:true) 
}

此代码的问题在于 activeTextfield 与视图的文本字段配合得很好,当我单击文本字段时会打印这些点

activetextfield Frame
(0.0, 20.5, 150.0, 20.5)

但是当我点击集合视图笔尖文本字段时,我得到了这些点

0.0, 0.0, 259.5, 30.0

我相信这是键盘阻止文本字段的原因,activetextfieldRect 给了错误的坐标

ScrollView.scrollRectToVisible(activeTextFieldRect!, animated:true)  

有人可以就如何解决这个问题给我一些指导吗?

【问题讨论】:

    标签: ios swift uiscrollview uitextfield


    【解决方案1】:
    func keyboardWillBeShown(sender: NSNotification) {
        print("KEYBOARD SHOWN")
        let info: NSDictionary = sender.userInfo!
        let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as! NSValue
        let keyboardSize: CGSize = value.CGRectValue().size
        let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
        ScrollView.contentInset = contentInsets
        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.ScrollView.frame
        aRect.size.height -= keyboardSize.height
        let activeTextFieldRect: CGRect? = activeTextField?.frame
        let activeTextFieldOrigin: CGPoint? = TextfieldPoint
        if (!CGRectContainsPoint(aRect, activeTextFieldOrigin!)) {
    
    
            ScrollView.scrollRectToVisible(testBox, animated:true)
        }
    
           }
    
    // Called when the UIKeyboardWillHideNotification is sent
    func keyboardWillBeHidden(sender: NSNotification) {
        print("KEYBOARD HIDDEN")
        let contentInsets: UIEdgeInsets = UIEdgeInsetsZero
        ScrollView.contentInset = contentInsets
        ScrollView.scrollIndicatorInsets = contentInsets
    }
    
    
    func textFieldDidBeginEditing(textField: UITextField) {
    
    
    
        let point: CGPoint = textField.convertPoint(CGPointZero, toView: self.ScrollView);
    
    TextfieldPoint = point
    
        testBox = CGRectMake(point.x, point.y, textField.frame.width, 100 );
    
        //let indexPath:NSIndexPath? = collview1.indexPathForItemAtPoint(point)
    
        print("TEXTFIELD EDIT")
        activeTextField = textField
    
        print(activeTextField)
    
    
    
    
    
        ScrollView.scrollEnabled = true
    }
    
    func textFieldDidEndEditing(textField: UITextField) {
    
        print("TEXTFIELD STOP EDIT")
        activeTextField = nil
        ScrollView.scrollEnabled = false
    }
    

    我想它真的很乱,我知道文本字段所在的位置,用它制作了 CGRect,如果键盘与文本重叠,视图会调整,在 iPhone 4s 到 6s plus 上测试,工作正常

    【讨论】:

      【解决方案2】:

      是的,你是对的。 scrollRectToVisible 收到错误的坐标,这就是它不起作用的原因。

      要实现您的目标,请考虑使用scrollToItemAtIndexPath(_:atScrollPosition:animated:)。您所需要的只是要移动的单元格的IndexPath。我猜你可以把这些信息放在你的textFieldtag 属性中(例如)。

      类似这样的:

      // inside cellForItem..
      cell.textField.tag = indexPath.item
      
      // inside keyboardWillShown
      let indexPath = NSIndexPath(forItem: activeTextField.tag inSection: 0) // assume that you have one section
      collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Top, animated: true) // or try .Center position
      

      【讨论】:

      • 这里的问题是你一次只能看到一个索引路径(相当大)n所有文本字段都有相同的索引路径。项目编号,我有这个以不同的方式工作,会尽快发布答案,感谢您的帮助
      猜你喜欢
      • 2012-01-30
      • 2020-02-23
      • 1970-01-01
      • 2010-10-01
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      相关资源
      最近更新 更多