第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用Swift使用鍵盤移動(dòng)視圖

使用Swift使用鍵盤移動(dòng)視圖

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

3 回答

?
慕少森

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊

這是一個(gè)解決方案,無需處理從一個(gè)textField到另一個(gè)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使用以下代碼替換這兩個(gè)函數(shù):

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


查看完整回答
反對 回復(fù) 2019-07-27
?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊

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

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

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

  3. 完成!

該對象將與鍵盤同步自動(dòng)向上移動(dòng)。


查看完整回答
反對 回復(fù) 2019-07-27
?
動(dòng)漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊

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

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

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

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

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

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

我們將使它在所有設(shè)備上看起來都很漂亮,并處理用戶添加或刪除預(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 {
        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)}

根據(jù)評論中的問題進(jìn)行更新:

如果您有兩個(gè)或更多文本字段,則可以檢查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)}


查看完整回答
反對 回復(fù) 2019-07-27
  • 3 回答
  • 0 關(guān)注
  • 854 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)