[ SWIFT ] - 虛擬鍵盤

在App 中虛擬鍵盤的使用,主要分為 3種情況:
(1)虛擬鍵盤開啟 時觸發下列的事件:
         UIKeyboardWillShowNotification
         UIKeyboardDidShowNotification
(2)隱藏虛擬鍵盤
         UIKeyboardWillHideNotification
         UIKeyboardDidHideNotification
(3)變更虛擬鍵盤
         UIKeyboardWillChangeFrameNotification
         UIKeyboardDidChangeFrameNotification

注意事項1:
由於不同設備中虛擬鍵盤的尺寸高度不一定一樣,因此必須在不同設備中取的虛擬鍵盤的尺寸,對此可以透過UIKeyboardFrameEndUserInfoKey來取得目前的鍵盤尺寸。
參考語法:
 let keyboardRectAsObject =notification.userInfo![UIKeyboardFrameEndUserInfoKey] as NSValue

注意事項2:虛擬鍵盤使用時會載入到Memory 中,因此當不使用虛擬鍵盤時,須清除Memory。
參考語法:
  deinit{
            NSNotificationCenter.defaultCenter().removeObserver(self)
           }

呼叫虛擬鍵盤得方法:

func handleKeyboardDidShow (notification: NSNotification){
 
    /* Get the frame of the keyboard */
    let keyboardRectAsObject =
    notification.userInfo![UIKeyboardFrameEndUserInfoKey] as NSValue
   
    /* Place it in a CGRect */
    var keyboardRect = CGRectZero
   
    keyboardRectAsObject.getValue(&keyboardRect)
 
    /* Give a bottom margin to our text view that makes it
    reach to the top of the keyboard */
    textView!.contentInset =
      UIEdgeInsets(top: defaultContentInset.top,
        left: 0, bottom: keyboardRect.height, right: 0)
  }
 
  func handleKeyboardWillHide(notification: NSNotification){
    textView!.contentInset = defaultContentInset
  }

//呼叫虛擬鍵盤
NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "handleKeyboardDidShow:",
        name: UIKeyboardDidShowNotification,
        object: nil)
     
//隱藏虛擬鍵盤     
      NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "handleKeyboardWillHide:",
        name: UIKeyboardWillHideNotification,
        object: nil)

以下為完整程式碼:
import UIKit

class ViewController: UIViewController {
 
  var textView: UITextView?
 
  let defaultContentInset =
  UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
 
  func handleKeyboardDidShow (notification: NSNotification){
 
    /* Get the frame of the keyboard */
    let keyboardRectAsObject =
    notification.userInfo![UIKeyboardFrameEndUserInfoKey] as NSValue
   
    /* Place it in a CGRect */
    var keyboardRect = CGRectZero
   
    keyboardRectAsObject.getValue(&keyboardRect)
 
    /* Give a bottom margin to our text view that makes it
    reach to the top of the keyboard */
    textView!.contentInset =
      UIEdgeInsets(top: defaultContentInset.top,
        left: 0, bottom: keyboardRect.height, right: 0)
  }
 
  func handleKeyboardWillHide(notification: NSNotification){
    textView!.contentInset = defaultContentInset
  }
 
  override func viewDidLoad() {
    super.viewDidLoad()
   
    textView = UITextView(frame: view.bounds)
    if let theTextView = textView{
      theTextView.text = "Some text goes here..."
      theTextView.font = UIFont.systemFontOfSize(16)
      theTextView.contentInset = defaultContentInset
     
      view.addSubview(theTextView)
     
      NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "handleKeyboardDidShow:",
        name: UIKeyboardDidShowNotification,
        object: nil)
     
     
      NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "handleKeyboardWillHide:",
        name: UIKeyboardWillHideNotification,
        object: nil)
     
    }
   
  }
 
  deinit{
    NSNotificationCenter.defaultCenter().removeObserver(self)
  }
 
}

留言

熱門文章