Swift 4:键盘显示时移动uiview

时间:2018-02-06 21:28:06

标签: ios swift

好 我有一个Uiviewcontroller,在这个viewcontroller中我把àuiview称为categUIV。 categoriesUIV包含uitexfields和一个按钮。这个想法是在键盘启动时向上移动孔类别。

我使用下面的代码

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)            
}   

func keyboardWillShow(notification: NSNotification) {            
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
    self.categUIV.frame.origin.y -= keyboardSize.height
}            
}

 func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
    self.categUIV.frame.origin.y += keyboardSize.height
}
 }

现在,当我点击一个uitextfied键盘弹出并且CategUIV正确弹出时,pb就是当我开始输入categUIV时,它会自动返回到他的初始位置。

我不知道为什么,但我想知道是因为分类的限制?

我真的需要帮助。 谢谢

2 个答案:

答案 0 :(得分:2)

问题是框架布局的东西不能按预期工作,尝试自动布局,将CategUIV的底部约束挂钩为IBOutlet并执行此操作

 @objc  func handleKeyboardDidShow (notification: NSNotification)
 {
    let keyboardRectAsObject = notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue

    var keyboardRect = CGRect.zero

    keyboardRectAsObject.getValue(&keyboardRect)

    self.categUIVBotcon.constant = -1 * keyboardRect.height 

    UIView.animate(withDuration: 0.5,animations: {

       self.view.layoutIfNeeded()

    })

} 

@objc func handleKeyboardWillHide(notification: NSNotification)
{

    self.categUIVBotcon.constant = 0

    UIView.animate(withDuration: 0.5,animations: {

       self.view.layoutIfNeeded()

    })

}

答案 1 :(得分:0)

要使其工作,您需要将视图放入滚动视图,以便您的层次结构如下所示:

- View Controller
  - View
    - Scroll View
      - Content View
        - categUIV 

您还必须将滚动视图和categUIV连接到您的代码:

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var categUIV: UIView!

然后您可以注册键盘观察员:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

具有以下功能:

func keyboardWasShown(notification: NSNotification) {
    self.keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
    self.scrollView.isScrollEnabled = true
    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, self.keyboardSize.height, 0.0)
    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
    var rect = self.view.frame
    rect.size.height -= self.keyboardSize.height
    if !rect.contains(self.categUIV.frame.origin) {
        self.scrollView.scrollRectToVisible(self.categUIV.frame, animated: true)
    }
}

func keyboardWillBeHidden(notification: NSNotification) {
    let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize.height, 0.0)
    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
    self.scrollView.isScrollEnabled = false
}
相关问题