出现时键盘高度会有所不同

时间:2017-02-21 04:37:31

标签: ios swift nsnotifications

我试图通过将底部约束修改为键盘高度来显示键盘时查看我的视图。但返回给我的键盘高度是变化的。

当我点击模拟器中的文本字段时,键盘高度为<?php require('fpdf.php'); class PDF extends FPDF { function WordWrap(&$text, $maxwidth) { $text = trim($text); if ($text==='') return 0; $space = $this->GetStringWidth(' '); $lines = explode("\n", $text); $text = ''; $count = 0; foreach ($lines as $line) { $words = preg_split('/ +/', $line); $width = 0; foreach ($words as $word) { $wordwidth = $this->GetStringWidth($word); if ($wordwidth > $maxwidth) { // Word is too long, we cut it for($i=0; $i<strlen($word); $i++) { $wordwidth = $this->GetStringWidth(substr($word, $i, 1)); if($width + $wordwidth <= $maxwidth) { $width += $wordwidth; $text .= substr($word, $i, 1); } else { $width = $wordwidth; $text = rtrim($text)."\n".substr($word, $i, 1); $count++; } } } elseif($width + $wordwidth <= $maxwidth) { $width += $wordwidth + $space; $text .= $word.' '; } else { $width = $wordwidth + $space; $text = rtrim($text)."\n".$word.' '; $count++; } } $text = rtrim($text)."\n"; $count++; } $text = rtrim($text); return $text; } } $pdf=new PDF(); $pdf->AddPage(); define("DBHOST","localhost"); define("DBNAME","users"); define("DBUSER","root"); define("PASS",""); $db = new PDO('mysql:host='.DBHOST.';dbname='.DBNAME,DBUSER,PASS); $query = $db->prepare("SELECT name,email,mobile,address,language FROM users "); $query->execute(); $datas = $query->fetchAll(PDO::FETCH_ASSOC); $pdf->SetFont('Times','B',12); $pdf->Cell(20,7,'Name',1); $pdf->Cell(50,7,'Email',1); $pdf->Cell(30,7,'Mobile',1); $pdf->Cell(50,7,'Address',1); $pdf->Cell(30,7,'Language',1); $pdf->Ln(); $pdf->SetFont('Times','',12); foreach ($datas as $row) { $pdf->Cell(20,7,$row['name'],1); $pdf->Cell(50,7,$row['email'],1); $pdf->Cell(30,7,$row['mobile'],1); $text = "An example of a long word is: Supercalifragulistic"; $pdf->WordWrap($text,30); $pdf->Cell(50,7,$pdf->Write(4,$text),1); //$pdf->write("<br>"); $pdf->Cell(30,7,$row['language'],1); $pdf->Ln(); } //$pdf->SetFont('Arial','',12); // $text='An example of a long word is: Supercalifragulistic'; // $pdf->WordWrap($text,15); // $pdf->Cell(10,0,$pdf->Write(4,$text)); // $pdf->Write(5,"This paragraph has $nb lines:\n\n"); // $pdf->Write(4,$text); $pdf->Output(); ?> 。当我尝试打开和关闭软件键盘时,键盘出现时会显示302。为什么会这样?

260

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(FriendsViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil) func keyboardWillShow(notification: NSNotification) { print("Keyboard appearing") guard let keyboardHeight = (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardFrameBeginUserInfoKey)?.CGRectValue.size.height else { return } bottomConstraint.constant = keyboardHeight print("keyboard height : \(keyboardHeight)") self.view.layoutIfNeeded() } 的高度实际上是正确的高度,因为它完美地调整了我的视图。高度为260时,我的视图偏移太远了。

我的观点布局是。 302位于顶部,后面跟UITextField

3 个答案:

答案 0 :(得分:5)

修改了Matt的答案,原因是

他是对的,您需要使用UIKeyboardFrameEndUserInfoKey代替UIKeyboardFrameBeginUserInfoKey,因为

  1. UIKeyboardFrameEndUserInfoKey根据您在设置中设置的预设为您提供最终高度。

  2. UIKeyboardFrameEndUserInfoKey返回高度两次,第一个没有语言预测,你可以在键盘上方看到,下一个带谓词,如果它是从设置激活但UIKeyboardFrameBeginUserInfoKey没有返回预测栏。

  3. iPhone 5s

    中的切换高度

    enter image description here

答案 1 :(得分:1)

问题在于您正在查看UIKeyboardFrameBeginUserInfoKey。你想看的是UIKeyboardFrameEndUserInfoKey

答案 2 :(得分:0)

这就是我在swift 2中的表现  首先添加此功能:

    // Lifting the view up
func animateViewMoving (up:Bool, moveValue :CGFloat){
    let movementDuration:NSTimeInterval = 0.3
    let movement:CGFloat = ( up ? -moveValue : moveValue)
    UIView.beginAnimations( "animateView", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(movementDuration )
    self.view.frame = CGRectOffset(self.view.frame, 0,  movement)
    UIView.commitAnimations()
}

然后实施UITextFieldDelegate

    // MARK: - UITextFieldDelegate

func textFieldShouldReturn(textField: UITextField) -> Bool {
    // Hide the keyboard.
    textField.resignFirstResponder()
    return true
}

func textFieldDidEndEditing(textField: UITextField) {
    animateViewMoving(false, moveValue: 100)
    textField.resignFirstResponder()
}

func textFieldDidBeginEditing(textField: UITextField) {
    animateViewMoving(true, moveValue: 100)
}

你也可以像这样获得键盘的确切高度:

var viewLiftUpValue : CGFloat

func keyboardWillShow(notification:NSNotification) {

        let userInfo:NSDictionary = notification.userInfo!
        let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.CGRectValue()
        let keyboardHeight = keyboardRectangle.height
        viewLiftUpValue = keyboardHeight
    }

然后将其提供给animateViewMoving()函数