我正在学习 AccessoryViews 并测试 Apple 示例: 键盘配件

我试图显示附件视图,避免显示键盘,但我做不到:-(

我在 textViewShouldBeginEditing 中返回 NO 以避免键盘并在返回之前对 TextView 的大小调整进行动画处理,但没有任何反应。

我做错了什么?

- (void)viewWillAppear:(BOOL)animated {

    // Make the keyboard appear when the application launches.
    [super viewWillAppear:animated];
    [textView becomeFirstResponder];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView {
    NSLog(@"textViewShouldBeginEditing");
    /*
     You can create the accessory view programmatically (in code), in the same nib file as the view controller's main view, or from a separate nib file. This example illustrates the latter; it means the accessory view is loaded lazily -- only if it is required.
     */

    if (textView.inputAccessoryView == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"AccessoryView" owner:self options:nil];
        // Loading the AccessoryView nib file sets the accessoryView outlet.
        textView.inputAccessoryView = accessoryView;    
        // After setting the accessory view for the text view, we no longer need a reference to the accessory view.
        self.accessoryView = nil;
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];

    textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, textView.frame.size.height - 100);

    [UIView commitAnimations];    

    return NO;
}
有帮助吗?

解决方案

inputAccessoryView 用于在标准苹果虚拟键盘上方显示一个视图。如果您不允许显示屏幕键盘,则 inputAccessoryView 也不会出现。

如果您只想显示自定义键盘,而不是标准屏幕键盘,则应使用 inputView 而不是 inputAccessoryView。在这里查看 inputView 和 inputAccessoryView 之间的区别:http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextView_Class/Reference/UITextView.html

其他提示

如果您确实只想要一个附件视图,则必须将输入视图设置为没有高度的视图。

在 Xcode 6 上,ios 8.1 模拟器默认显示隐藏的键盘。(我必须选择“切换软件键盘”才能显示它。)

我喜欢这个,因为只有我的附件视图出现。

我有一种方法可以以编程方式重现此内容(打开和关闭键盘,不关闭)

谢谢!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top