工具栏不会显示在键盘上方

时间:2012-04-12 00:38:00

标签: iphone objective-c ios5 uitoolbar

我无法让工具栏显示在上面(就像苹果带有完成按钮一样)键盘。有什么可以想到为什么它没有显示?如下图所示。

enter image description here

·H

@interface CustomerNotesController : UITableViewController <UITextViewDelegate>
{
    UIToolbar *noteToolbar;
    IBOutlet UITextView *note;
    id delegate;
}

@property (nonatomic, strong) IBOutlet UITextView *note;
@property (nonatomic, retain) id delegate;
@property (nonatomic, retain) UIToolbar *noteToolbar;

的.m

@synthesize note, noteToolbar, delegate;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [note setDelegate:self];
    [note becomeFirstResponder];
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    NSLog(@"EDITING");
    if (noteToolbar == nil) {
        NSLog(@"Creating toolbar");

        noteToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 44.0)];
        noteToolbar.barStyle = UIBarStyleBlackTranslucent;

        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNote:)];

        UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered target:self action:@selector(clearNote:)];

        UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveNote:)];

        noteToolbar.items = [NSArray arrayWithObjects:cancelButton, clearButton, extraSpace, doneButton, nil];
        NSLog(@"Items: %@", noteToolbar.items);
    }

    NSLog(@"Current field: %@",textView);
    NSLog(@"keyboard: %@", noteToolbar);
    textView.inputAccessoryView = noteToolbar;
}

日志

2012-04-11 17:31:00.148 MyApp[28892:fb03] EDITING
2012-04-11 17:31:00.148 MyApp[28892:fb03] Creating toolbar
2012-04-11 17:31:00.149 MyApp[28892:fb03] Items: (
    "<UIBarButtonItem: 0x10dc3420>",
    "<UIBarButtonItem: 0x10dc3480>",
    "<UIBarButtonItem: 0x10dc34e0>",
    "<UIBarButtonItem: 0x10dc3540>"
)
2012-04-11 17:31:00.149 MyApp[28892:fb03] Current field: <UITextView: 0x10d94f40; frame = (5 5; 310 140); text = 'This is sample text'; clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x10d95170>; contentOffset: {0, 0}>
2012-04-11 17:31:00.150 MyApp[28892:fb03] keyboard: <UIToolbar: 0x10dc2820; frame = (0 0; 320 44); opaque = NO; layer = <CALayer: 0x10dc2730>>

3 个答案:

答案 0 :(得分:3)

//使用此方法

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

    textView.inputAccessoryView = yourToolBar;

    return YES;
}

答案 1 :(得分:3)

尝试使用viewDidLoad方法设置这些内容!

答案 2 :(得分:0)

你不能这样做。键盘是一个系统控制的UIView,你无法直接控制。操作系统将把键盘视图放在所有其他视图的顶部,以确保它具有正确的焦点来收集用户输入。

您能否提供更多有关您希望这样做的背景信息?即使您确实找到了一种方法来使视图按照您的描述行事,但几乎肯定会被Apple拒绝违反人机界面指南。