iphone - 如何替换 iOS 6 上 UIWebView 键盘下工具栏上的按钮?

标签 iphone button uiwebview toolbar ios6

如何在 iOS 6 上替换 UIWebView 键盘下工具栏上的按钮?

以下代码在 iOS 5.1 上运行良好,但在 iOS 6 上不起作用:

UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
    if (![[testWindow class] isEqual:[UIWindow class]]) {
        keyboardWindow = testWindow;
        break;
    }
}
for (UIView *possibleFormView in [keyboardWindow subviews]) {
    // iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView.
    if ([[possibleFormView description] rangeOfString:@"UIPeripheralHostView"].location != NSNotFound) {
        for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) {
            if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIWebFormAccessory"].location != NSNotFound) {
                UIBarButtonItem *buttonDone =[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(pressDone)];
                NSArray *itemsArray;
                itemsArray = [NSArray arrayWithObjects:buttonDone, nil];
                [(UIToolbar*)subviewWhichIsPossibleFormView setItems:itemsArray];
            }
        }
    }
}

iOS 6 上的错误:

2012-09-27 16:31:13.537 Linux[2633:907] -[UIWebFormAccessory setItems:]: unrecognized selector sent to instance 0x1d886ad0
2012-09-27 16:31:13.540 Linux[2633:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIWebFormAccessory setItems:]: unrecognized selector sent to instance 0x1d886ad0'
*** First throw call stack:
(0x361032a3 0x3441397f 0x36106e07 0x36105531 0x3605cf68 0x775c5 0x33bbda6f 0x360d85df 0x360d8291 0x360d6f01 0x36049ebd 0x36049d49 0x365862eb 0x37428301 0x7538d 0x75328)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

非常感谢您的帮助!

最佳答案

UIWebFormAccessory 不再是 iOS6 上的 UIToolbar。不过,它包含您正在寻找的 UIToolbar。使用类似以下内容在任一操作系统版本上查找它...

+ (UIToolbar *)findVirginWebKeyboardToolbar:(UIView *)parent
{
    if ([parent isKindOfClass:[UIToolbar class]]) {
        // the stock toolbar contains a single item with a UISegmentedControl customView.
        UIToolbar *tb = (UIToolbar *)parent;
        if ([tb.items count] == 1 && [((UIBarButtonItem *)[tb.items objectAtIndex:0]).customView isKindOfClass:[UISegmentedControl class]]) {
            return tb;
        }
    }

    for (UIView *view in parent.subviews) {
        UIToolbar *tb = [self findVirginWebKeyboardToolbar:view];
        if (tb) return tb;
    }

    return nil;
}

关于iphone - 如何替换 iOS 6 上 UIWebView 键盘下工具栏上的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12622931/

相关文章:

asp.net - 为按钮设置垂直对齐

iphone - 如何使 UIWebView 平滑滚动?

iphone - UITapGestureRecognizer 忽略网络链接请求

iphone - 在 UIWebView 中从 JS 返回一个 var

iphone - 登录时的按钮操作 FBConnect API 中的按钮单击

iphone - 在显示 View 之前加载 iPhone 键盘

Android 按钮背景颜色

c# - 如何回调主线程以启用来自后台工作线程的按钮

iphone - UIscrollview调整大小问题

iphone - UITableView : moving rows around in editing mode