Swift使键盘适应设备屏幕

时间:2015-09-03 10:03:28

标签: swift keyboard tableview frame


所以我想学习swift,我正在制作这个简单的聊天应用程序......我在控制键盘方面遇到了一些问题。

  • 键盘出现时我想向上推框架。如果我不这样做,我将无法访问我的文本域。我发现这两个功能能够做到这一点......问题是设备屏幕不一样,如果我在另一台设备上运行应用程序,我必须改变大小......
    例如,如果我使用iPhon6,我将使用220,如果我使用5则需要150 ......如果我没记错的话。维度并不重要,事情就是不一样......

我认为这必须是一种轻松控制未来的方法......我会感激任何帮助......

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);
      }
      //hide keyboard and get down the view
      func keyboardWillHide(sender: NSNotification) {
        self.view.frame.origin.y += 200
      }
      //show keyboard and get up the view
      func keyboardWillShow(sender: NSNotification) {
        self.view.frame.origin.y -= 200
      }

      //hide keyboard if you tap the screen
      override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        self.view.endEditing(true)
      }

谢谢!

1 个答案:

答案 0 :(得分:1)

  

添加ScrollView尝试以下代码。

 @IBOutlet weak var scrollView: UIScrollView!
 @IBOutlet weak var onTopScrollView: UIView!
 var activeTextField: UITextField!
 var keyboardHeight: CGFloat = 0

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);
  }
  //hide keyboard and get down the view
  func keyboardWillHide(sender: NSNotification) {
    keyboardHeight = 0
    self.activeTextField = nil
    let contentInsets = UIEdgeInsetsZero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
    scrollView.contentOffset = CGPointMake(0, 0)
  }
  //show keyboard and get up the view
  func keyboardWillShow(sender: NSNotification) {
    let info : NSDictionary = sender.userInfo!
    let kbSize = (info.objectForKey(UIKeyboardFrameBeginUserInfoKey)?.CGRectValue() as CGRect!).size
    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    keyboardHeight = kbSize.height
    println(keyboardHeight)

    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    var aRect = self.onTopScrollView.frame
    aRect.size.height -= keyboardHeight;
    if (!CGRectContainsPoint(aRect, self.activeTextField.frame.origin) ) {
        var rect = onTopScrollView.convertRect(CGRectZero, fromView: self.activeTextField)
        rect.size = self.activeTextField.frame.size
        //print(CGRectContainsPoint(aRect, self.activeTextField.frame.origin))
        self.scrollView.scrollRectToVisible(rect, animated: true)
    }
  }

   override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self)
    super.viewWillDisappear(animated)
}

// handle TextField Delegates 
 func textFieldShouldReturn(textField: UITextField) -> Bool {

    textField.resignFirstResponder()
    return true
}

func textFieldDidBeginEditing(textField: UITextField) {
    self.activeTextField = textField
}