2012-08-16 22 views
31

Czy możliwe jest dynamiczne uzyskanie ramy, a właściwie jej wysokości? Ponieważ mam UITextView i chciałbym dostosować jego wysokość stosownie do wysokości klatki klawiatury, gdy metoda wprowadzania klawiatury zostanie zmieniona. Jak wiadomo, różne metody wprowadzania mogą mieć inną wysokość klatki klawiatury.Dynamicznie uzyskaj ramkę klawiatury

Odpowiedz

97

spróbuj tego:

[[NSNotificationCenter defaultCenter] addObserver:self 
            selector:@selector(keyboardWasShown:) 
             name:UIKeyboardDidShowNotification 
             object:nil]; 

- (void)keyboardWasShown:(NSNotification *)notification 
{ 

// Get the size of the keyboard. 
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

//Given size may not account for screen rotation 
int height = MIN(keyboardSize.height,keyboardSize.width); 
int width = MAX(keyboardSize.height,keyboardSize.width); 

//your other code here.......... 
} 

Tutorial for more information

+1

Jest to szczególnie przydatne podczas pracy z różnymi klawiaturami wysokości/typu w systemie iOS8. Do tej pory wystarczyło tylko hardcoding 216 (portret). Dzięki. – n00bProgrammer

+2

na ios 8 nadal zachowuje 216, co jest błędne. – harshitgupta

+0

@Hector Czy to również działa na iOS8? – msmq

8

Po prostu wykonaj ten samouczek od Apple, a dostaniesz to, co chcesz. Apple Documentation. Aby określić obszar objęty klawiaturą, zapoznaj się z tym tutorial.

+0

Ale jak mogę wykryć akcję zmiany metody wprowadzania? –

+0

Jeśli jesteś programistą, powinieneś wiedzieć, która klawiatura wejściowa została otwarta przez ciebie w danym momencie. – doNotCheckMyBlog

+3

Chciałbym wiedzieć, ale nie mogłem. Jeśli wiesz, jak wykryć metodę zmiany metody wprowadzania i uzyskać wysokość aktualnej klawiatury, czy możesz uprzejmie mnie poinformować. Naprawdę to doceniam :) –

2

Dla Swift 3 użytkowników, kod @Hector (z pewnymi dodatkami) byłoby:

w twojej viewDidLoad dodać obserwatora:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(_:)), name: .UIKeyboardDidShow , object: nil) 
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidHide(_:)), name: .UIKeyboardDidHide , object: nil) 

Następnie zaimplementuj następujące metody:

func keyboardDidShow(_ notification: NSNotification) { 
    print("Keyboard will show!") 
    // print(notification.userInfo) 

    let keyboardSize:CGSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size 
    print("Keyboard size: \(keyboardSize)") 

    let height = min(keyboardSize.height, keyboardSize.width) 
    let width = max(keyboardSize.height, keyboardSize.width) 

} 

func keyboardDidHide(_ notification: NSNotification) { 
     print("Keyboard will hide!") 
} 
0

Możesz dodać ten kod do widoku, który zawiera pole tekstowe w Swift 3. Spowoduje to, że pole tekstowe będzie animowane w górę iw dół za pomocą klawiatury.

private var keyboardIsVisible = false 
private var keyboardHeight: CGFloat = 0.0 

// MARK: Notifications 

private func registerForKeyboardNotifications() { 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
} 
private func deregisterFromKeyboardNotifications() { 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
} 

// MARK: Triggered Functions 

@objc private func keyboardWillShow(notification: NSNotification) { 
    keyboardIsVisible = true 
    guard let userInfo = notification.userInfo else { 
     return 
    } 
    if let keyboardHeight = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.height { 
     self.keyboardHeight = keyboardHeight 
    } 
    if !textField.isHidden { 
     if let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber, 
      let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber { 
      animateHUDWith(duration: duration.doubleValue, 
          curve: UIViewAnimationCurve(rawValue: curve.intValue) ?? UIViewAnimationCurve.easeInOut, 
          toLocation: calculateTextFieldCenter()) 
     } 
    } 
} 

@objc private func keyboardWillBeHidden(notification: NSNotification) { 
    keyboardIsVisible = false 
    if !self.isHidden { 
     guard let userInfo = notification.userInfo else { 
      return 
     } 
     if let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber, 
      let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber { 
      animateHUDWith(duration: duration.doubleValue, 
          curve: UIViewAnimationCurve(rawValue: curve.intValue) ?? UIViewAnimationCurve.easeInOut, 
          toLocation: calculateTextFieldCenter()) 
     } 
    } 
} 

// MARK: - Helpers 

private func animateHUDWith(duration: Double, curve: UIViewAnimationCurve, toLocation location: CGPoint) { 
    UIView.beginAnimations(nil, context: nil) 
    UIView.setAnimationDuration(TimeInterval(duration)) 
    UIView.setAnimationCurve(curve) 
    textField.center = location 
    UIView.commitAnimations() 
} 

private func calculateTextFieldCenter() -> CGPoint { 
    if !keyboardIsVisible { 
     return self.center 
    } else { 
     let yLocation = (self.view.frame.height - keyboardHeight)/2 
     return CGPoint(x: self.center.x, y: yLocation) 
    } 
} 
Powiązane problemy