簡體   English   中英

顯示或隱藏鍵盤時調整UIView

[英]Adjusting a UIView when the keyboard shows or hides

我正在添加鍵盤觀察器,以在顯示鍵盤時調整視圖高度。 但是,當文本字段位於視圖的頂部並顯示鍵盤時,文本字段會上升得更多。 有人可以指導我如何正確解決此問題,因為在執行大量搜索后,他們都建議相同的內容。 我在iPhone 5上使用iOS 9。

這是我的代碼。 viewDidLoad()

//SET OBSERVER WHEN KEYBOARD IS SHOWN
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: self.view.window)

//SET OBSERVER WHEN KEYBOARD IS HIDDEN
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: self.view.window)

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

//HANDLING KEYBOARD APPEARING AND DISAPPEARING
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

    print(("\(keyboardSize.height)---\(offset.height)"))  **//this is always showing the values 216.0 for both** 

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

您正在采用復雜的方法來解決不是很困難的問題。 請記住,手機的尺寸多種多樣,因此嘗試移動視圖對於一種設備(例如iPhone 5)可能效果很好,但在其他設備(例如iPhone 4,iPhone 6s)上體驗較差。

您正在尋找的解決方案是將控件放在UIScrollView 然后,當鍵盤顯示或隱藏時,您不會移動任何內容–您只需調整滾動視圖的內容插圖,然后讓iOS進行其余操作。

由於還存在可以隨意連接和拔出的硬件鍵盤,因此顯示和隱藏的鍵盤變得很復雜。 在(極其廣泛的!)測試中,無論鍵盤發生什么情況,我都設法將似乎在所有情況下均能正常運行的代碼組合在一起。 我嘗試將其縮小,並總是發現它無法正常工作的邊緣情況,因此我采用了這種固定方法,效果很好。 如果您能夠使用UIScrollView ,則此確切方法也將為您工作。

步驟1:注冊正確的通知。 具體來說,將其放入viewDidLoad()

let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "adjustForKeyboard:", name: UIKeyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: "adjustForKeyboard:", name: UIKeyboardWillChangeFrameNotification, object: nil)

步驟2:將此方法添加到您的視圖控制器中:

func adjustForKeyboard(notification: NSNotification) {
    let userInfo = notification.userInfo!

    let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)

    if notification.name == UIKeyboardWillHideNotification {
        yourScrollView.contentInset = UIEdgeInsetsZero
    } else {
        yourScrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
    }

    yourScrollView.scrollIndicatorInsets = yourScrollView.contentInset
}

步驟3:任何使用UITextView人都應該在該方法的末尾添加這兩行,以使用戶不會失去位置:

let selectedRange = yourScrollView.selectedRange
yourScrollView.scrollRangeToVisible(selectedRange)

…雖然在這種情況下大概是yourTextView或類似的東西。

暫無
暫無

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

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