4

是否可以在键盘上方创建 inputAccessoryView 以在拆分模式下全屏显示?系统会自动调整其大小以适合应用程序框架。这是iOS中的错误吗?

4

2 回答 2

1

您可以使用此方法来检测您的键盘是否处于拆分模式。 注意:为了让它工作,你需要UITextField/UITextView一个非零inputAccessoryView(你可以只使用一个空视图)。

NSArray *classPath = @[
  @"KeyboardAutomatic",
  @"KeyboardImpl",
  @"KeyboardLayoutStar",
  @"KBKeyplaneView",
  @"KBSplitImageView"
];
UIView *splitView = textField.inputAccessoryView.superview;
for (NSString *className in classPath) {
  for (UIView *subview in splitView.subviews) {
    if ([NSStringFromClass([subview class]) rangeOfString:className].location != NSNotFound) {
      splitView = subview;
      break;
    }
  }
}
BOOL isSplit = [splitView.subviews count] > 1;

然后您可以使用自定义 inputAccessoryView 并CGRectFrame根据键盘是否处于拆分模式进行编辑。这是一个很好的例子:https ://github.com/bmancini55/iOSExamples-DockedKeyboardView

于 2016-05-02T12:39:20.260 回答
-1

分享相同问题的已接受答案链接

UIKit 在显示键盘时发布 UIKeyboardWillShowNotification,在隐藏键盘时发布 UIKeyboardWillHideNotification。这些通知包含正确设置 UITextField 动画所需的一切。

假设您的 UITextField 位于名为 myTextField 的属性中。

首先,您需要在某处注册通知。您在哪里注册取决于负责移动 myTextField 的对象。在我的项目中,该字段的超级视图负责,并且由于我从 nib 加载 UI,因此我在超级视图的 awakeFromNib 中执行此操作:

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillShowNotification object:nil];
}

如果您使用 UIViewController 来移动字段,您可能希望在 viewWillAppear:animated: 中执行此操作。

您应该在您的 dealloc 或 viewWillDisappear:animated:: 中取消注册

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

当然,棘手的一点在于keyboardWillHideOrShow: 方法。首先我从通知中提取动画参数:

- (void)keyboardWillHideOrShow:(NSNotification *)note
{
    NSDictionary *userInfo = note.userInfo;
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];

    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

keyboardFrame 在全局坐标系中。我需要将frame转换成与myTextField.frame相同的坐标系,而myTextField.frame在myTextField.superview的坐标系中:

CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil];

接下来,我计算我希望 myTextField 移动到的帧。新框架的底部边缘应等于键盘框架的顶部边缘:

CGRect newTextFieldFrame = self.myTextField.frame;
newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height;

最后,我使用与键盘相同的动画参数将 myTextField 设置为新帧:

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
        self.myTextField.frame = newTextFieldFrame;
    } completion:nil];
}

这里全部放在一起:

- (void)keyboardWillHideOrShow:(NSNotification *)note
{
    NSDictionary *userInfo = note.userInfo;
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];

    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil];

    CGRect newTextFieldFrame = self.myTextField.frame;
    newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height;

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
        self.myTextField.frame = newTextFieldFrame;
    } completion:nil];
}
于 2016-05-04T09:31:28.783 回答