【问题标题】:Is it possible to create inputAccessoryView above the keyboard for the full width of the screen in the split mode?是否可以在键盘上方创建 inputAccessoryView 以在拆分模式下全屏显示?
【发布时间】:2016-05-24 18:20:26
【问题描述】:

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

【问题讨论】:

  • 我不了解 ui。你能添加一张图片吗?以便我给你更好的答案
  • 同意@Code Hunter。
  • 你到底想要什么?

标签: ios objective-c ipad inputaccessoryview splitview


【解决方案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

【讨论】:

    【解决方案2】:

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

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

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

    首先,您需要在某处注册通知。您在哪里注册取决于负责移动 myTextField 的对象。在我的项目中,该字段的 superview 负责,并且由于我从 nib 加载 UI,因此我在 superview 的 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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多