所以我遵循了关于在键盘上方移动文本的苹果指南,当文本字段在scrollview上时这很好用,但是scrollview包含一个集合视图,该集合视图加载一个带有填充在其中的文本字段的笔尖,它们都被指定为委托,当它们被按下时,委托的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)
但是当我点击集合视图nib文本字段时,我得到了这些点
0.0, 0.0, 259.5, 30.0
我相信这是键盘阻塞文本字段的原因,activetextfieldRect给出了错误的坐标
ScrollView.scrollRectToVisible(activeTextFieldRect!, animated:true)
有人可以给我一些方向来解决这个问题吗?
答案 0 :(得分:1)
是的,你是对的。 scrollRectToVisible
收到错误的坐标,这就是为什么它不起作用。
要实现您的目标,请考虑使用scrollToItemAtIndexPath(_:atScrollPosition:animated:)。你所需要的只是移动的IndexPath
个细胞。我想你可以将这些信息放在tag
的{{1}}属性中(例如)。
这样的事情:
textField
答案 1 :(得分:0)
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加,工作正常