亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用Swift使用鍵盤移動視圖

使用Swift使用鍵盤移動視圖

四季花海 2019-07-27 20:00:05
使用Swift使用鍵盤移動視圖我有一個應用程序,在視圖的下半部分有一個文本字段。這意味著當我輸入文本字段時鍵盤覆蓋文本字段。如何在打字時向上移動視圖以便我可以看到我正在鍵入的內容,然后在鍵盤消失時將其移回原來的位置?我到處都看,但所有解決方案似乎都在Obj-C中,我還沒有完全轉換。任何幫助將不勝感激。
查看完整描述

3 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

這是一個解決方案,無需處理從一個textField到另一個textField的切換:

 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.view.frame.origin.y -= keyboardSize.height    }            }func keyboardWillHide(notification: NSNotification) {
    self.view.frame.origin.y = 0}

要解決此問題,請keyboardWillShow/Hide使用以下代碼替換這兩個函數:

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

編輯SWIFT 3.0:

override func viewDidLoad() {
    super.viewDidLoad()            
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)    }@objc func keyboardWillShow(notification: NSNotification) {        
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0 {
            self.view.frame.origin.y -= keyboardSize.height        }
    }        }@objc func keyboardWillHide(notification: NSNotification) {
    if self.view.frame.origin.y != 0 {
        self.view.frame.origin.y = 0
    }}

編輯SWIFT 4.0:

override func viewDidLoad() {
    super.viewDidLoad()            
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)    }@objc func keyboardWillShow(notification: NSNotification) {        
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0 {
            self.view.frame.origin.y -= keyboardSize.height        }
    }        }@objc func keyboardWillHide(notification: NSNotification) {
    if self.view.frame.origin.y != 0 {
        self.view.frame.origin.y = 0
    }}

編輯SWIFT 4.2:

override func viewDidLoad() {
    super.viewDidLoad()            
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)}@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0 {
            self.view.frame.origin.y -= keyboardSize.height        }
    }}@objc func keyboardWillHide(notification: NSNotification) {
    if self.view.frame.origin.y != 0 {
        self.view.frame.origin.y = 0
    }}


查看完整回答
反對 回復 2019-07-27
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

最簡單的方法,甚至不需要任何代碼:

  1. 如果您還沒有使用Spring動畫框架,請下載KeyboardLayoutConstraint.swift并將文件添加(拖放)到您的項目中。

  2. 在故事板中,為視圖或文本字段創建底部約束,選擇約束(雙擊它),然后在Identity Inspector中,將其類從NSLayoutConstraint更改為KeyboardLayoutConstraint。

  3. 完成!

該對象將與鍵盤同步自動向上移動。


查看完整回答
反對 回復 2019-07-27
?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

此線程上的一個流行答案使用以下代碼:

func keyboardWillShow(sender: NSNotification) {
    self.view.frame.origin.y -= 150}func keyboardWillHide(sender: NSNotification) {
    self.view.frame.origin.y += 150}

將靜態量偏移視圖存在明顯問題。它在一臺設備上看起來不錯,但在任何其他尺寸配置上看起來都很糟糕。您需要獲得鍵盤高度并將其用作偏移值。

這是一個適用于所有設備的解決方案,可以處理用戶在鍵入時隱藏預測文本字段的邊緣情況。

需要注意的是,我們將self.view.window作為對象參數傳遞。這將為我們提供鍵盤數據,例如它的高度!

@IBOutlet weak var messageField: UITextField!override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: self.view.window)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: self.view.window)}func keyboardWillHide(sender: NSNotification) {
    let userInfo: [NSObject : AnyObject] = sender.userInfo!
    let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size    self.view.frame.origin.y += keyboardSize.height}

我們將使它在所有設備上看起來都很漂亮,并處理用戶添加或刪除預測文本字段的情況。

func keyboardWillShow(sender: NSNotification) {
    let userInfo: [NSObject : AnyObject] = sender.userInfo!
    let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size    let offset: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue.size    if keyboardSize.height == offset.height {
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.view.frame.origin.y -= keyboardSize.height        })
    } else {
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.view.frame.origin.y += keyboardSize.height - offset.height        })
    }}

刪除觀察員

在離開視圖之前不要忘記刪除觀察者,以防止傳輸不必要的消息。

override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: self.view.window)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: self.view.window)}

根據評論中的問題進行更新:

如果您有兩個或更多文本字段,則可以檢查view.frame.origin.y是否為零。

func keyboardWillShow(sender: NSNotification) {
    let userInfo: [NSObject : AnyObject] = sender.userInfo!

    let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size    let offset: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue.size    if keyboardSize.height == offset.height {
        if self.view.frame.origin.y == 0 {
            UIView.animateWithDuration(0.1, animations: { () -> Void in
                self.view.frame.origin.y -= keyboardSize.height            })
        }
    } else {
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.view.frame.origin.y += keyboardSize.height - offset.height        })
    }
     print(self.view.frame.origin.y)}


查看完整回答
反對 回復 2019-07-27
  • 3 回答
  • 0 關注
  • 855 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號