键盘向上移动时如何修复UIView的位置?

时间:2018-01-11 20:00:42

标签: ios swift uiview keyboard uitextfield

如何在键盘向上移动时修复imagelabeltextField的位置?

我制作了一个代码,当键盘显示时向上移动func textFieldDidEndEditing(_ textField: UITextField) { moveTextField(textField: rasstoyanietextField, moveDistance: -215, up: false) } func textFieldDidBeginEditing(_ textField: UITextField) { moveTextField(textField: rasstoyanietextField, moveDistance: -215, up: true) } func textFieldShouldReturn(_ textField: UITextField) -> Bool { rasstoyanietextField.resignFirstResponder() return true } func moveTextField(textField: UITextField, moveDistance: Int, up: Bool){ let moveDuration = 0.1 let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance) UIView.beginAnimations("animateTextField", context: nil) UIView.setAnimationBeginsFromCurrentState(true) UIView.setAnimationDuration(moveDuration) self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement) UIView.commitAnimations() } ,但这会移动所有内容。

$bar_chart_data = array(
    array(
        "Data1" => 548.25,
        "Data2" => 238.75,
        "Data3" => 95.50,
        "Data4" => 300.50,
        "Data5" => 286.80,
        "Data6" => 148.25
    ),
    array(

    )
);

$sumArray = array(); // Create an empty array to store the sum of each sub-array
foreach ($bar_chart_data as $subArray) {
    $sum = 0; // Initialize sum as zero
    foreach ($subArray as $key=>$value) {
        $sum += $value;
    }
    $sumArray[] = $sum; // Append each sum to the array
}

print_r($sumArray); // Print the array containing the sums.

我该如何解决?

2 个答案:

答案 0 :(得分:0)

添加keybaord观察员

  NotificationCenter.default.addObserver(
                self,
                selector: #selector(handleKeyboardDidShow),
                name: NSNotification.Name.UIKeyboardDidShow,
                object: nil)


            NotificationCenter.default.addObserver(
                self,
                selector: #selector(handleKeyboardWillHide),
                name:NSNotification.Name.UIKeyboardWillHide,
                object: nil)

我认为最好在viewControllers中使用scrollview,它们有textfeilds / textViews在显示时向上移动所有项目但是在这里你可以勾选textfeild的底部约束或它的superview并将其拖动为IBOutlet并执行此操作

 @objc func handleKeyboardDidShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {

          self.textFeilfBottomCon.constant = -1 * keyboardSize.height

          UIView.animate(withDuration: 0.5, animations: {

              self.view.layoutIfNeeded()

         })


    }
}

@objc func handleKeyboardWillHide(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {

            self.textFeilfBottomCon.constant = 0

             UIView.animate(withDuration: 0.5, animations: {

              self.view.layoutIfNeeded()

         })

    }
}

答案 1 :(得分:0)

因为您移动整个视图,而不是仅移动textview。更改行:

self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)

使用:

textField.frame = textField.frame.offsetBy(dx: 0, dy: movement)