无需 UIKeyboardWillShowNotification 即可获取键盘大小

发布于 2024-11-27 08:42:19 字数 1105 浏览 0 评论 0原文

我想在不使用可用键盘通知的情况下获取键盘的大小。原因是我在视图上有几个文本字段,并且我不需要调整所有文本字段的视图大小,正如我在几乎每个示例中看到的那样。我只需要调整编辑时位于键盘后面的某些文本字段/视图的视图大小。所以我使用 textFieldDidBeginEditingtextFieldDidEndEditing 方法,因为在这里我知道正在编辑什么文本字段。另一个问题是,即使我订阅键盘通知,UIKeyboardWillShowNotification 也会在 textFieldDidBeginEditing 之后触发,因此我无法在第一次激活时获取键盘大小。我假设键盘通知功能未提供任何信息,其中实际文本字段或视图可用。

The following code works but I need the keyboard size:

- (void) textFieldDidBeginEditing:(UITextField *) textField {
  if ([theControls containsObject: textField]) {
    [UIView beginAnimations: @"szar" context: nil];
    [UIView setAnimationDuration:0.3];
    self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, -216);
    [UIView commitAnimations];
  }
}

- (void) textFieldDidEndEditing:(UITextField *) textField {
  if ([theControls containsObject: textField]) {
    [UIView beginAnimations: @"szar" context: nil];
    [UIView setAnimationDuration:0.3];
    self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, +216);
    [UIView commitAnimations];
  }
}

I want to get the size of the keyboard without using the keyboard notifications available. The reason is I have several text field on the view and I don't need to resize the view for all of them, as I have seen in nearly every example. I just need to resize the view for some of the text fields/views that will be behind the keyboard when editing. So I'm using textFieldDidBeginEditing and
textFieldDidEndEditing methods, because here I know what textfield is being edited. Another problem is that even if I subscribe to the keyboard notifications the UIKeyboardWillShowNotification is fired after textFieldDidBeginEditing so I cannot get the keyboard size on the first activation. I assume no information is provided from the keyboard notification functions where the actual text field or view is abailable.

The following code works but I need the keyboard size:

- (void) textFieldDidBeginEditing:(UITextField *) textField {
  if ([theControls containsObject: textField]) {
    [UIView beginAnimations: @"szar" context: nil];
    [UIView setAnimationDuration:0.3];
    self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, -216);
    [UIView commitAnimations];
  }
}

- (void) textFieldDidEndEditing:(UITextField *) textField {
  if ([theControls containsObject: textField]) {
    [UIView beginAnimations: @"szar" context: nil];
    [UIView setAnimationDuration:0.3];
    self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, +216);
    [UIView commitAnimations];
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2024-12-04 08:42:19

我仍然使用通知,但没有使用您指定的通知。也许这会更好?只是想提供帮助,我了解这些事情是多么令人沮丧。

viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWasShown:)
                                         name:UIKeyboardDidShowNotification
                                       object:nil];
 //For Later Use
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWillHide:)
                                         name:UIKeyboardWillHideNotification
                                       object:nil];
}

- (void)keyboardWasShown:(NSNotification *)notification {
  // Get the size of the keyboard.
  CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
}

编辑:这也可能有助于区分活动文本字段和非活动文本字段

- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.activeTextField = textField;
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
self.activeTextField = nil;
}

I still used Notifications but not the ones you specified against. Maybe this will work better? Just trying to help, I understand how frustrating these things can be.

viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWasShown:)
                                         name:UIKeyboardDidShowNotification
                                       object:nil];
 //For Later Use
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWillHide:)
                                         name:UIKeyboardWillHideNotification
                                       object:nil];
}

- (void)keyboardWasShown:(NSNotification *)notification {
  // Get the size of the keyboard.
  CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
}

Edit: This May Help As Well To Distinguish Active Text Fields From The Non-Active

- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.activeTextField = textField;
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
self.activeTextField = nil;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文