簡體   English   中英

是否可以在分割模式下在鍵盤上方創建inputAccessoryView以獲得整個屏幕寬度?

[英]Is it possible to create inputAccessoryView above the keyboard for the full width of the screen in the split mode?

是否可以在分割模式下在鍵盤上方創建inputAccessoryView以獲得整個屏幕寬度? 系統會自動調整大小以適應應用程序框架。 這是iOS中的錯誤嗎?

您可以使用此方法檢測鍵盤是否處於拆分模式。 注意:為了使其工作,您需要一個帶有非零inputAccessoryViewUITextField/UITextView (您可以只使用一個空視圖)。

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

共享相同問題的已接聽答案鏈接

UIKit在顯示鍵盤時發布UIKeyboardWillShowNotification,在隱藏鍵盤時發布UIKeyboardWillHideNotification。 這些通知包含正確設置UITextField動畫所需的一切。

假設您的UITextField位於名為myTextField的屬性中。

首先,您需要在某處注冊通知。 您注冊的位置取決於移動myTextField的對象。 在我的項目中,該字段的superview負責,因為我從一個筆尖加載我的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位於全局坐標系中。 我需要將幀轉換為與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];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM