1

所以我有一个我正在尝试构建的非常简单的应用程序。它基本上是一个可滚动的文本字段,可以填满整个屏幕。

当用户点击屏幕时,键盘会出现,您可以开始在您点击的行上进行编辑。

如果您点击将出现键盘的区域,则键盘的大小会textView缩小,因此您不会在键盘后面输入文本。

直到这里的一切都有效。生病把代码放在底部。

当用户完成编辑时,'Done'屏幕右上角有一个按钮,当他们按下该按钮时,键盘应该消失,并且,如果文本量超过屏幕上的大小,无论它们是哪一行编辑应该在屏幕的底部。

现在,无论我尝试什么,当我resignFirstResponder隐藏键盘时,textView重置contentOffset(0,0)

我在屏幕最底部输入文本时的视图

当我在上面的视图中时,我按下完成,结果如下:

在此处输入图像描述

我想要发生的是我正在编辑的位置位于屏幕底部,如下所示:

在此处输入图像描述

类变量,以便可以从文件中的任何位置访问它们

var textField: UITextView = UITextView()

var withKeyboard: NSLayoutConstraint!
var withoutKeyboard: NSLayoutConstraint!


override func viewDidLoad() {
    super.viewDidLoad()

    self.view.addSubview(textField)

    textField.scrollEnabled = true
    textField.selectable = true
    textField.bounces = true
    textField.contentMode = UIViewContentMode.Bottom
    textField.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.withoutKeyboard = NSLayoutConstraint(item: self.textField, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)

    self.view.addConstraint(NSLayoutConstraint(item: self.textField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
    self.view.addConstraint(self.withoutKeyboard)
    self.view.addConstraint(NSLayoutConstraint(item: self.textField, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: self.textField, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))

 }


func keyboardWillShow(notification: NSNotification){
    doneButton.hidden = false
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        keyboardHeight = keyboardSize.height
        self.withKeyboard = NSLayoutConstraint(item: self.textField, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -keyboardHeight)
        self.view.removeConstraint(withoutKeyboard)
        self.view.addConstraint(withKeyboard)
        textField.layoutIfNeeded()
    }
}


func keyboardWillHide(notification: NSNotification){
    self.doneButton.hidden = true
    self.textFieldOffset = self.textField.contentOffset.y - self.keyboardHeight
    println(self.textFieldOffset)
    self.view.removeConstraint(withKeyboard)
    self.view.addConstraint(withoutKeyboard)
    textField.layoutIfNeeded()
    textField.contentOffset.y = self.textFieldOffset
}

func donePressed(){
    textField.resignFirstResponder()
    createRecord()
}

我制作了withKeyboardandwithoutKeyboard约束,这样每当键盘出现/消失时,我就可以取下一个并添加另一个。

无论如何,当我点击完成按钮时,它会将视图重置到最顶部。这不是所有的代码,它只是给我带来麻烦的部分。

4

0 回答 0