虽然让它工作起来会很好,但我决定采取完全放弃它的方法,而是使用水龙头来关闭键盘。
我的印象是隐藏它也会隐藏自动更正栏。事实并非如此。
以下是我正在使用的完整代码。
键盘的最终颜色代码,由我的应用设置中的切换开关调用。
NSUserDefaults *darkDefaults = [NSUserDefaults standardUserDefaults];
BOOL darkOn = [darkDefaults boolForKey:@"darkKeyboard"];
if (darkOn) {
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual : [UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
// Locate UIWebFormView.
for (UIView *possibleFormView in [keyboardWindow subviews]) {
if ([possibleFormView isKindOfClass:NSClassFromString(@"UIInputSetContainerView")]) {
for (UIView* peripheralView in possibleFormView.subviews) {
//Keyboard background
for (UIView* peripheralView_sub in peripheralView.subviews) {
peripheralView_sub.backgroundColor = [UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:0.75];
}
}
}
}
}
else{
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual : [UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
// Locate UIWebFormView.
for (UIView *possibleFormView in [keyboardWindow subviews]) {
if ([possibleFormView isKindOfClass:NSClassFromString(@"UIInputSetContainerView")]) {
for (UIView* peripheralView in possibleFormView.subviews) {
//Keyboard background
for (UIView* peripheralView_sub in peripheralView.subviews) {
peripheralView_sub.backgroundColor = [UIColor clearColor];
}
}
}
}
}
隐藏键盘。在我的视图控制器顶部调用(可能不是苹果安全的,但我不需要发布,所以它对我有用):
@interface UIWebBrowserView : UIView
@end
@implementation UIWebBrowserView (CustomToolbar)
- (id)inputAccessoryView {
return nil;
}
@end
现在从我的测试来看,我可以通过在 inputAccessoryView 部分中绘制一个新视图或工具栏来为它着色,但是点击以消除它的混乱,需要进行一些调整,但这不是我的正常标准栏去。哦,好吧。
如果您想像我在表格视图中所做的那样实现切换,我就是这样做的。
- (id)init{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadTableView:)
name:NSUserDefaultsDidChangeNotification object:nil];
return [self initWithStyle:UITableViewStyleGrouped];
}
cellForRowAtIndexPath:
[cell.textLabel setText:@"Dark Keyboard"];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
darkKeyboard = [[UISwitch alloc] initWithFrame: CGRectMake(7, 0, 0, 0)];
cell.accessoryView = [[UIView alloc] initWithFrame:darkKeyboard.frame];
[cell.accessoryView addSubview:darkKeyboard];
[self.darkKeyboard addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventValueChanged];
//On Color
darkKeyboard.onTintColor = [UIColor colorWithRed:0.204 green:0.667 blue:0.863 alpha:0.85];
//Off Color
darkKeyboard.backgroundColor = [UIColor colorWithRed:0.678 green:0.161 blue:0.188 alpha:0.75];
darkKeyboard.TintColor = [UIColor clearColor];
darkKeyboard.layer.cornerRadius = 16.0;
//Risize
darkKeyboard.transform = CGAffineTransformMakeScale(1.1, 1.1);
//User defaults
darkKeyboard.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"darkKeyboard"];
UIView *keyboard = [[UIView alloc] initWithFrame:cell.frame];
keyboard.backgroundColor = [UIColor colorWithRed:0.176 green:0.176 blue:0.176 alpha:1];
cell.backgroundView = keyboard;
didSelectRowAtIndexPath:
刚刚加了个NSLog,这里什么都不需要。
- (void)updateSwitchAtIndexPath:(id)sender {
if (darkKeyboard){
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:self.darkKeyboard.on forKey:@"darkKeyboard"];
[userDefaults synchronize];
if ([darkKeyboard isOn]) {
[darkKeyboard setOn:YES animated:YES];
[self.tableView reloadData];
[[UIApplication sharedApplication] reloadInputViews];
} else {
[darkKeyboard setOn:NO animated:YES];
[self.tableView reloadData];
[[UIApplication sharedApplication] reloadInputViews];
}
}
}
希望这对某人有所帮助。