【问题标题】:How can I move the view up for last text field when keyboard appears?当键盘出现时,如何向上移动最后一个文本字段的视图?
【发布时间】:2017-04-28 12:10:42
【问题描述】:

我有一个视图,里面有 6 个文本字段,但我想在单击最后一个文本字段时向上移动视图,我可以在最后一个文本字段中输入值。

这是我的代码,但它适用于所有文本字段:-

 func animateViewMoving (up:Bool, moveValue :CGFloat){

    let movementDuration:TimeInterval = 0.3
    let movement:CGFloat = ( up ? -moveValue : moveValue)

    UIView.beginAnimations("animateView", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(movementDuration)

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

那么对于特定的文本字段,我该如何向上移动视图(键盘大小)

谢谢

【问题讨论】:

  • 你可以使用自动布局吗?
  • 您可以使用 IQKeyboardManager 解决此问题。
  • 嘿,最好使用 IQKeyBoardManager。它将负责滚动视图本身。
  • IQKeyBoardManager 是一个库?
  • @kishor0011 是的,它是图书馆。

标签: ios swift view keyboard uitextfield


【解决方案1】:
@IBOutlet var scrollView: UIScrollView!
@IBOutlet var contentView: UIView!
var textFieldActive:UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    //Keyboard notification
    NotificationCenter.default.addObserver(self,selector: #selector(keyboardWasShown),name:NSNotification.Name.UIKeyboardDidShow,
                                                     object: nil)
    NotificationCenter.default.addObserver(self,selector: #selector(keyboardWillHide),name:NSNotification.Name.UIKeyboardWillHide,
                                                     object: nil)
    // Do any additional setup after loading the view.
}



func textFieldDidBeginEditing(_ textField: UITextField){

    textFieldActive = textField
}



// MARK: - Keyboard notification Method
func keyboardWasShown(notification: NSNotification)
{
    let userInfo = notification.userInfo
    let keyboardSize = (userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
    let contentInset = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize?.height)!+100,  0.0)
    self.scrollViewGroupMakeTakers.contentInset = contentInset
    self.scrollViewGroupMakeTakers.scrollIndicatorInsets = contentInset

    // Step 3: Scroll the target text field into view.
    var aRect: CGRect = self.contentViewMakeTakers.frame;
    aRect.size.height -= (keyboardSize?.height)!+100
    if (!aRect.contains(textFieldActive!.frame.origin))
    {

        let scrollPoint: CGPoint = CGPoint(x: 0.0, y: textFieldActive!.frame.origin.y - (keyboardSize!.height-150));            
self.scrollViewGroupMakeTakers.setContentOffset(scrollPoint, animated: true)
    }
}
func keyboardWillHide(notification: NSNotification)
{
    let contentInsets : UIEdgeInsets = UIEdgeInsets.zero
    scrollViewGroupMakeTakers.contentInset = contentInsets;
    scrollViewGroupMakeTakers.scrollIndicatorInsets = contentInsets;
}

【讨论】:

  • @kishor0011 如果您发现这是您的答案。接受它作为答案
  • 我的意思是它不适用于最后一个文本字段。它适用于所有文本字段。谢谢
  • @kishor0011 意味着您只需要最后一个文本字段对吗?
【解决方案2】:

试试这个-

 func textFieldShouldReturn(textField: UITextField) -> Bool {

     if textField == lastTextField{

            view.frame.origin.y -= 50  //Or 100, 200 as you required
             performAction()

        }
}



func performAction() {

        lastTextField.becomeFirstResponder()
        otherTextField.resignFirstResponder()

    }

【讨论】:

  • 并且不要忘记在最后一个 textField 辞职时设置view.frame.origin.y = 0
  • 我知道这一点,但我想要键盘尺寸。之后我可以向上移动视图。谢谢
  • 这里不是 50 我怎样才能得到键盘尺寸?谢谢
【解决方案3】:

假设您使用的是 autoLayout

我想象你所有的UITextField 都存在于一个单独的UIScrollView 中。此外,这个UISCrollView 将对其 superView 有一个底部约束。为这个底部间距垂直约束创建一个IBOutlet 到它的超级视图。

注意:即使它不在UIScrollView 内,您也可以通过简单地采用最低UITextField 的底部约束来执行此操作,但这会迫使您的另一个UITextFields 最好关闭屏幕把它放在UIScrollView中。

在您的viewDidLoad 设置观察者如下 -

func viewDidLoad(){

    super.viewDidLoad()

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

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

//and then:
//MARK: Animate Keyboard
func keyboardWillShow(notification : NSNotification){

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {

        animateKeyboard(withConstraintValue: keyboardSize.size.height)
    }
}

func keyboardWillHide(notification : NSNotification){

    animateKeyboard(withConstraintValue: 0)
}

func animateKeyboard(withConstraintValue: CGFloat){

    scrollViewBottomSpaceConstraint.constant = withConstraintValue

    UIView.animate(withDuration: 0.3, animations: {

        self.view.layoutIfNeeded()
    })
}

【讨论】:

  • 其实我没有使用滚动视图。我的要求是当我进入最后一个文本字段时,我必须向上移动视图。除了这个,一切都很好。
  • 请阅读说明:)。我已经添加了你可以通过简单地接受最后一个UITextField的底部约束来做同样的事情!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-21
  • 2018-12-26
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
相关资源
最近更新 更多