【问题标题】:UIToolbar not adding to KeyboardUIToolbar 未添加到键盘
【发布时间】:2011-06-11 17:48:54
【问题描述】:

我正在尝试将 UIToolbar 添加到 UIKeyboard,以便用户名可以在两个文本字段之间轻松切换。但是我无法让它工作。请你能告诉我我做错了什么。我已经在 viewDidLoad 中设置了 NotificationCenter。

- (void)keyboardWillShow:(NSNotification *)notification {
    for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
        // Now iterating over each subview of the available windows
        for (UIView *keyboard in [keyboardWindow subviews]) {
            // Check to see if the description of the view we have referenced is UIKeyboard.
            // If so then we found the keyboard view that we were looking for.
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
                NSValue *v = [[notification userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
                CGRect kbBounds = [v CGRectValue];
                UIToolbar *input = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kbBounds.size.width, 50)];
                UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStylePlain target:self action:@selector(previousTextField:)];
                UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextTextField:)];
                UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
                UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideKeyboard:)];
                [input setItems:[NSArray arrayWithObjects:previous,next,space,done,nil]];
                if(input == nil) {
                    UIToolbar *input = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kbBounds.size.width, 50)];
                    UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStylePlain target:self action:@selector(previousTextField:)];
                    UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextTextField:)];
                    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
                    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideKeyboard:)];
                    [input setItems:[NSArray arrayWithObjects:previous,next,space,done,nil]];
                }
                [input removeFromSuperview];
                input.frame = CGRectMake(0, 0, kbBounds.size.width, 30);
                [keyboard addSubview:input];
                keyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y, kbBounds.size.width, kbBounds.size.height + 60);
                for(UIView* subKeyboard in [keyboard subviews]) {
                    if([[subKeyboard description] hasPrefix:@"<UIKeyboardImpl"] == YES) {
                        subKeyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y - 30, kbBounds.size.width, kbBounds.size.height);
                    }
                }
            }
        }
    }
} 

【问题讨论】:

    标签: uitextfield uitoolbar uikeyboard


    【解决方案1】:

    创建UIToolbar 的实例,在工具栏中添加您需要的任何视图,然后在UITextFieldinputAccessoryView 属性中分配您的工具栏实例。

    myTextFieldorTextView.inputAccessoryView = boolbar;
    

    查看以下分步指南,将工具栏放在键盘顶部。

    Adding a Toolbar with Next & Previous above UITextField Keyboard

    【讨论】:

      猜你喜欢
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多