键盘将 UIView 推开

Keyboard is pushing UIView away

在我的 iOS 应用程序中,我使用了多个 UIView,其中一次只显示 1 个。我通过按钮在这些视图之间切换。

正在显示一个新的:

self.viewPage2.frame = CGRectMake(0,0,320,568)

隐藏视图:

self.viewPage2.frame = CGRectMake(500,0,320,568)

其中一个 UIView 包含 UITextField。当我单击此文本字段时,键盘打开但 UIView 被推离屏幕。

我预计它与 textField 有关,所以我包含了该源代码。我有来自

的代表
@IBOutlet weak var textFieldSearchValue: UITextField!

func textFieldDidEndEditing(textField: UITextField) {
    textFieldSearchValue.resignFirstResponder();
}

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    return true;
}

func textFieldShouldReturn(textField: UITextField) -> Bool {

    textFieldSearchValue.resignFirstResponder();
    return true;
}

在 ViewDidLoad 中:

textFieldSearchValue.delegate = self

所以奇怪的是,当打开键盘时,UIView 被向上推,显示 ViewController 的初始屏幕,没有我的 UIViews。 UITextField 也被推离屏幕!

由于您正在寻找自动布局解决方案,因此您应该考虑框架更新,例如

self.viewPage2.frame = CGRectMake(500,0,320,568)

自动布局不支持。

您必须更新约束条件才能实现上述目标。 例如:

self.myViewBottomSpaceConstraint.constant += 256;
[self.myView updateConstraints];

如果您想要超过 1 个文本字段,最好将它们嵌入到 scrollView 或 tableView 中。他们会减轻你的痛苦。