gpt4 book ai didi

ios - 自定义 webview 键盘问题

转载 作者:可可西里 更新时间:2023-11-01 05:28:23 25 4
gpt4 key购买 nike

因此从这个线程改编代码 UIKeyboardAppearance in UIWebViewTomSwift's很棒的答案,我得到了大约 99% 的答案。

在 iOS 7 模拟器中,一切似乎都运行良好。然而在 iOS 8 中,当键盘第一次出现时,< > 完成栏是白色的。当我点击或选择另一个输入时,它会更改为我指定的颜色。

我的问题是,如何防止和/或更改该白色部分?

White Bar Dark Bar

其他线程中的所有代码都是相同的,除了我在 keyboardWillAppear 中这样调用的颜色。

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 description] hasPrefix : @"<UIInputSetContainerView"]) {

for (UIView* peripheralView in possibleFormView.subviews) {
peripheralView.backgroundColor = [UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:0.75];

for (UIView* peripheralView_sub in peripheralView.subviews) {
peripheralView_sub.backgroundColor = [UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:0.75];

}
}
}
}

如有任何帮助,我们将不胜感激。

最佳答案

所以随着 iOS 9+ 的发布,我发现它破坏了上述方法。但是通过一些修补和查看一些观点,我想出了一个补充我已经在下面回答的内容。

现在我决定放弃自定义颜色的东西,我正在挖掘适合我的应用程序的黑色键盘。无论如何,这对我有用。在 9.1 sim 到 7 上测试。也在我的 6+ 上运行 9.0.2。

//Keyboard setting
@interface UIWebBrowserView : UIView
@end
@interface UIWebBrowserView (UIWebBrowserView_Additions)
@end
@implementation UIWebBrowserView (UIWebBrowserView_Additions)
- (id)inputAccessoryView {
return nil;
}

- (UIKeyboardAppearance) keyboardAppearance{

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL switchOn = [userDefaults boolForKey:@"darkKeyboard"];

if (switchOn) {
return UIKeyboardAppearanceDark;
}
else {
return UIKeyboardAppearanceDefault;
}
}
@end

@interface UITextInputTraits : UIWebBrowserView
@end
@interface UITextInputTraits (UIWebBrowserView)
@end
@implementation UITextInputTraits (UIWebBrowserView)
- (UIKeyboardAppearance) keyboardAppearance{

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL switchOn = [userDefaults boolForKey:@"darkKeyboard"];

if (switchOn) {
return UIKeyboardAppearanceDark;
}
else {
return UIKeyboardAppearanceDefault;
}
}
@end

我真的希望有人觉得这些答案有帮助 :D

更新信息:对完成栏很好奇,这就是这一切的开始。我重新启用它只是为了看看,发现它把它变成了黑色。不错的奖励,尽管我放弃了它以使用滚动隐藏键盘。

15 年 12 月 19 日更新所以我决定从 UIWebView 过渡到 WKWebView,结果发现两者之间显然有所不同。我设法让它再次工作。常规的 UIKeyboardAppearanceDark 调用会导致键盘比我喜欢的更透明。所以我根据自己的喜好修改了它。同样,这可能不是标准的做事方式,但我不在乎,反正它不会成为苹果。

所有内容仍在 AppDelegate 中。

//Removing the input bar above the keyboard.
@interface InputHider : NSObject @end
@implementation InputHider
-(id)inputAccessoryView{
return nil;
}
@end

@interface UIWebBrowserView : NSObject
@end
@interface NSObject (UIWebBrowserView_Additions)
@end
@implementation NSObject (UIWebBrowserView_Additions)
- (UIKeyboardAppearance) keyboardAppearance{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL switchOn = [userDefaults boolForKey:@"darkKeyboard"];

if (switchOn) {
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")] ||
[possibleFormView isKindOfClass:NSClassFromString(@"UIInputSetHostView")]) {
for (UIView* peripheralView in possibleFormView.subviews) {
peripheralView.backgroundColor = [UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1.0];

//Keyboard background
for (UIView* peripheralView_sub in peripheralView.subviews) {
peripheralView_sub.backgroundColor = [UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1.0];

//Accessory bar color
if ([possibleFormView isKindOfClass:NSClassFromString(@"WKContentView")]) {
for (UIView* UIInputViewContent_sub in peripheralView_sub.subviews) {
[[UIInputViewContent_sub layer] setOpacity : 1.0];
UIInputViewContent_sub.backgroundColor = [UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1.0];

}
}
}
}
}
}
return UIKeyboardAppearanceDark;
}
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")] || [possibleFormView isKindOfClass:NSClassFromString(@"UIKeyboard")]) {
for (UIView* peripheralView in possibleFormView.subviews) {
peripheralView.backgroundColor = [UIColor clearColor];

//Keyboard background
for (UIView* peripheralView_sub in peripheralView.subviews) {
peripheralView_sub.backgroundColor = [UIColor clearColor];

//Accessory bar color
if ([possibleFormView isKindOfClass:NSClassFromString(@"UIWebFormAccessory")]) {
for (UIView* UIInputViewContent_sub in peripheralView_sub.subviews) {
[[UIInputViewContent_sub layer] setOpacity : 1.0];
UIInputViewContent_sub.backgroundColor = [UIColor clearColor];

}
}
}
}
}
}
return UIKeyboardAppearanceDefault;
}
}
@end

@interface UITextInputTraits : UIWebBrowserView
@end
@interface UITextInputTraits (UIWebBrowserView)
@end
@implementation UITextInputTraits (UIWebBrowserView)
- (UIKeyboardAppearance) keyboardAppearance{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL switchOn = [userDefaults boolForKey:@"darkKeyboard"];

if (switchOn) {
return UIKeyboardAppearanceDark;
}
else {
return UIKeyboardAppearanceDefault;
}
}
@end

//Disables endDisablingInterfaceAutorotationAnimated error for keyboard
@interface UIWindow (UIWebBrowserView)
- (void)beginDisablingInterfaceAutorotation;
- (void)endDisablingInterfaceAutorotation;
@end

@implementation UIWindow (UIWebBrowserView)
- (void)beginDisablingInterfaceAutorotation {}
- (void)endDisablingInterfaceAutorotation{}
@end

我还没有像以前那样设法找到隐藏 inputAccessoryBar 的方法,但多亏了几个线程,我才让它工作。在我的 View Controller 中,我调用:

-(void)removeInputAccessoryView {
UIView* subview;

for (UIView* view in webView.scrollView.subviews) {
if([[view.class description] hasPrefix:@"WKContent"])
subview = view;
}

if(subview == nil) return;

NSString* name = [NSString stringWithFormat:@"%@SwizzleHelper", subview.class.superclass];
Class newClass = NSClassFromString(name);

if(newClass == nil)
{
newClass = objc_allocateClassPair(subview.class, [name cStringUsingEncoding:NSASCIIStringEncoding], 0);
if(!newClass) return;

Method method = class_getInstanceMethod([AppDelegate class], @selector(inputAccessoryView));
class_addMethod(newClass, @selector(inputAccessoryView), method_getImplementation(method), method_getTypeEncoding(method));

objc_registerClassPair(newClass);
}

object_setClass(subview, newClass);
}

在我调用的 viewDidLoad 中:

[self removeInputAccessoryView];

我计划再进行一些修补,但就目前而言,它可以满足我的需要。

关于ios - 自定义 webview 键盘问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28664984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com