【问题标题】:UIView Above Keyboard not Displaying键盘上方的 UIView 不显示
【发布时间】:2013-11-24 20:08:07
【问题描述】:

我正在使用Apple KeyBoardAccessory Documentation 在我的键盘上方添加一个 UIView。问题是 UIView 根本没有显示。下面是我的代码:

头文件:

@property (nonatomic, weak) IBOutlet UITextView *textView;
@property (nonatomic, weak) IBOutlet UIView *accessoryView;

@property (nonatomic, weak) IBOutlet UIBarButtonItem *editButton;
@property (nonatomic, weak) IBOutlet UIBarButtonItem *doneButton;

寻找键盘通知:

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.textView becomeFirstResponder];

    // observe keyboard hide and show notifications to resize the text view appropriately
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

    // set the right bar button item initially to "Edit" state
    self.navigationItem.rightBarButtonItem = self.editButton;
}


- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView {

    // you can create the accessory view programmatically (in code), or from the storyboard
    if (self.textView.inputAccessoryView == nil) {

        self.textView.inputAccessoryView = self.accessoryView;
    }

    self.navigationItem.rightBarButtonItem = self.doneButton;

    return YES;
}

终于想搬了textField

#

pragma mark - Responding to keyboard events

- (void)keyboardWillShow:(NSNotification *)notification {

    /*
     Reduce the size of the text view so that it's not obscured by the keyboard.
     Animate the resize so that it's in sync with the appearance of the keyboard.
     */

    NSDictionary *userInfo = [notification userInfo];

    // Get the origin of the keyboard when it's displayed.
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    // Get the top of the keyboard as the y coordinate of its origin in self's view's
    // coordinate system. The bottom of the text view's frame should align with the top
    // of the keyboard's final position.
    //
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect newTextViewFrame = self.view.bounds;
    newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;

    // Get the duration of the animation.
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];

    // Animate the resize of the text view's frame in sync with the keyboard's appearance.
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];

    self.textView.frame = newTextViewFrame;

    [UIView commitAnimations];
}

【问题讨论】:

  • 您确认self.accessoryView 不是nil
  • 我该如何解决这个问题? @rmaddy
  • 使用调试器或添加日志语句。
  • 我仍然一无所获@rmaddy 我使用教程中的相同代码,所以我不知道问题是什么。
  • 你有没有给accessoryView 出口分配任何东西?

标签: ios objective-c uiview uitextview


【解决方案1】:

您确定将 IBOutlet 附件视图分配给情节提要中的 UIView 吗? (这也是他们在教程中所做的,检查情节提要。)否则可以预期,什么都不会发生。

【讨论】:

  • 是的,但我仍然没有得到任何回应
【解决方案2】:

尝试使用在键盘出现时向上滚动视图的滚动视图。为了使这更容易,请尝试使用TPKeyboardAvoiding 库。它工作得很好,而且很容易设置:

  1. 将 UIScrollView 添加到视图控制器的 xib 中
  2. 将滚动视图的类设置为 TPKeyboardAvoidingScrollView(仍然 在 xib 中,通过身份检查器)
  3. 将所有控件放在该滚动视图中

如果您有多个文本字段或文本视图,它还会自动连接键盘上的“下一步”按钮来切换文本字段。

【讨论】:

    猜你喜欢
    • 2014-05-07
    • 2011-07-10
    • 2012-06-04
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 2016-01-13
    相关资源
    最近更新 更多