如何使用Apple iPhone SDK获取UIKeyboard大小

有没有办法以编程方式获得UIKeyboard大小。 高度216.0f,景观高度162.0f。

以下似乎被depricated。 有没有任何警告的方式在3.0 iPhone操作系统SDK和4.0 iPhone操作系统的SDK都可以这样做..

CGSize keyBoardSize = [[[note userInfo] objectForKey:UIKeyboardBoundsUserInfoKey]CGRectValue].size; 

谢谢,Tharindu

您可以使用UIKeyboardFrameBeginUserInfoKeyUIKeyboardFrameEndUserInfoKey代替userInfo字典中的键盘大小。

这两个键返回一个包含CGRectNSValue实例,该实例在键盘的显示/隐藏animation的开始点和结束点处都保存键盘的位置和大小。

编辑:

为了澄清, userInfo字典来自NSNotification实例。 它传递给你的方法,你注册一个观察员。 例如,

 - (void)someMethodWhereYouSetUpYourObserver { // This could be in an init method. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myNotificationMethod:) name:UIKeyboardDidShowNotification object:nil]; } - (void)myNotificationMethod:(NSNotification*)notification { NSDictionary* keyboardInfo = [notification userInfo]; NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey]; CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue]; } 

编辑2:

另外,请不要忘记在dealloc方法中以观察者身份移除自己! 这是为了避免通知中心在释放后通知对象时发生崩溃。

您应该使用UIKeyboardWillChangeFrameNotification ,因为有些国际键盘(如中文键盘)在使用过程中会更改框架。 另外,请确保将CGRect转换为正确的视图,以供横向使用。

 //some method like viewDidLoad, where you set up your observer. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil]; - (void)keyboardWillChange:(NSNotification *)notification { CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; //this is it! } 

以下是我最终做出的作品。 我结合不同答案的build议和代码。 特点:解除键盘,移动键盘上方的文本字段,同时编辑和设置“下一步”和“完成”键盘返回types。更多字段的“…”

 static const CGFloat ANIMATION_DURATION = 0.4; static const CGFloat LITTLE_SPACE = 5; CGFloat animatedDistance; CGSize keyboardSize; @interface ViewController () <UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *firstNameTXT; .....// some other text fields @property (weak, nonatomic) IBOutlet UITextField *emailTXT; @end @implementation ViewController - (void)viewDidLoad{ ..... // add tap gesture to help in dismissing keyboard UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapScreen:)];// outside textfields [self.view addGestureRecognizer:tapGesture]; // set text fields return key type to Next, last text field to Done [self.firstNameTXT setReturnKeyType:UIReturnKeyNext]; ..... [self.emailTXT setReturnKeyType:UIReturnKeyDone]; // set text fields tags [self.firstNameTXT setTag:0]; ....// more text fields [self.emailTXT setTag:5]; // add keyboard notification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; } // dismiss keyboard when tap outside text fields - (IBAction)tapScreen:(UITapGestureRecognizer *)sender { if([self.firstNameTXT isFirstResponder])[self.firstNameTXT resignFirstResponder]; ... if([self.emailTXT isFirstResponder])[self.emailTXT resignFirstResponder]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField{ if(textField.returnKeyType==UIReturnKeyNext) { // find the text field with next tag UIView *next = [[textField superview] viewWithTag:textField.tag+1]; [next becomeFirstResponder]; } else if (textField.returnKeyType==UIReturnKeyDone || textField.returnKeyType==UIReturnKeyDefault) { [textField resignFirstResponder]; } return YES; } // Moving current text field above keyboard -(BOOL) textFieldShouldBeginEditing:(UITextField*)textField{ CGRect viewFrame = self.view.frame; CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField]; CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view]; CGFloat textFieldBottomLine = textFieldRect.origin.y + textFieldRect.size.height + LITTLE_SPACE;// CGFloat keyboardHeight = keyboardSize.height; BOOL isTextFieldHidden = textFieldBottomLine > (viewRect.size.height - keyboardHeight)? TRUE :FALSE; if (isTextFieldHidden) { animatedDistance = textFieldBottomLine - (viewRect.size.height - keyboardHeight) ; viewFrame.origin.y -= animatedDistance; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:ANIMATION_DURATION]; [self.view setFrame:viewFrame]; [UIView commitAnimations]; } return YES; } -(void) restoreViewFrameOrigionYToZero{ CGRect viewFrame = self.view.frame; if (viewFrame.origin.y != 0) { viewFrame.origin.y = 0; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:ANIMATION_DURATION]; [self.view setFrame:viewFrame]; [UIView commitAnimations]; } } -(void)keyboardDidShow:(NSNotification*)aNotification{ NSDictionary* info = [aNotification userInfo]; keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; } -(void)keyboardDidHide:(NSNotification*)aNotification{ [self restoreViewFrameOrigionYToZero];// keyboard is dismissed, restore frame view to its zero origin } @end 

在迅速4

 override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(getInfo(notif:)), name: .UIKeyboardDidShow , object: nil) } 

接着:

 @objc func getInfo(notif: NSNotification) -> Void { guard let userInfo = notif.userInfo else {return} if let myData = userInfo["UIKeyboardFrameBeginUserInfoKey"] as? CGRect { print(myData.width) print(myData.height) } }