问题
How to replace buttons on a toolbar under UIWebView
keyboard on iOS 6?
The following code works fine on iOS 5.1 but doesn't work on 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];
}
}
}
}
The error on 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)
Thanks a lot for the help!
回答1:
UIWebFormAccessory is no longer a UIToolbar on iOS6. It contains the UIToolbar you're looking for, though. Use something like the following to find it on either OS version...
+ (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;
}
回答2:
Use the following code to remove unused toolbar:
[subviewWhichIsPossibleFormView removeFromSuperview];
And add a new one. Sorry, no code available.
来源:https://stackoverflow.com/questions/12622931/how-to-replace-buttons-on-a-toolbar-under-uiwebview-keyboard-on-ios-6