簡體   English   中英

Swift 4:當鍵盤顯示時移動 uiview

[英]Swift 4 : moving uiview when keyboard shows

我有一個 Uiviewcontroller,在這個 viewcontroller 中我放了一個名為 categUIV 的 uiview。 categUIV 包含 uitexfields 和一個按鈕。 這個想法是在鍵盤抬起時向上移動 categUIV 孔。

我使用下面的代碼

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 正確彈出,問題是當我開始鍵入時,categUIV 會自行返回到他的初始位置。

我不知道為什么,但我想知道這可能是因為 categUIV 的限制嗎?

問題是框​​架布局的東西不能按預期工作,嘗試自動布局,將 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()

    })

}

要完成這項工作,您需要將您的視圖放入滾動視圖中,以便您的層次結構如下所示:

- 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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM