如果出现键盘,则自动对齐表示

时间:2016-12-14 02:33:10

标签: ios swift xcode autolayout

我无法处理autolayout,我需要实现类似于facebook / instagram的登录页面。也就是说,当您单击输入字段登录名或密码时,键盘和所有表示都会适应窗口的新大小。

enter image description here

键盘出现后,它看起来像这样: enter image description here

func updateBottomLayoutConstraintWithNotification(_ notification: Notification) {
    let userInfo = notification.userInfo!

    let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
    let keyboardEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let convertedKeyboardEndFrame = view.convert(keyboardEndFrame, from: view.window)
    let rawAnimationCurve = (notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).uint32Value << 16
    let animationCurve = UIViewAnimationOptions.init(rawValue: UInt(rawAnimationCurve))

    bottomLayoutConstraint.constant = (view.bounds).maxY - convertedKeyboardEndFrame.minY

    UIView.animate(withDuration: animationDuration, delay: 0.0, options: animationCurve, animations: {
        self.view.layoutIfNeeded()
    }, completion: nil)
}

1 个答案:

答案 0 :(得分:0)

Autolayout的提示:

  1. 使用static table view float degree = Math.round(sensorEvent.values[0]); tvHeading.setText(Float.toString(degree) + " degrees"); RotateAnimation ra = new RotateAnimation( of, -degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // how long the animation will take place ra.setDuration(210); // set the animation after the end of the reservation status ra.setFillAfter(true); // Start the animation image.startAnimation(ra);
  2. 如果你使用静态表视图所有滚动和键盘相关的东西由表视图本身处理。
  3. 如果使用滚动视图,只需从顶部元素到最终元素开始提供约束,并使用this库,它非常有用且易于使用。见下图
  4. 谢谢:) enter image description here