UIWebView Keyboard - Getting rid of the “Previous/Next/Done” bar

后端 未结 9 2085
清酒与你
清酒与你 2020-12-13 07:05

I want to get rid of the bar on top of the keyboard that appears when you focus a text field in a webview. We have some other ways of handling this and it\'s redundant and

相关标签:
9条回答
  • 2020-12-13 07:57

    There are no public APIs for doing this. You could remove it by examining the view hierarchy and removing the view as some have suggested, but this would be very risky.

    Here's why it's a bad idea:

    If Apple doesn't have an official API for removing the bar, they may have good reasons for doing so, and their own code may rely on it being there. You might not ever encounter a problem because you do all your testing (for example) on an English keyboard. But what if the view you are removing is required for entry in another language, or for accessibility purposes? Or what if in a future version of iOS their own implementation changes such that it assumes the view is always there? Your code will crash, and you'll be stuck scrambling to get an update out while frustrated users wait for weeks.

    Interestingly, Remco's appended answer proves this point. On iOS 6.0.1, a change was made that required a fix to the hack. Anyone who had implemented the hack for ios 5 would have been forced to do an update as a result. Fortunately it was only an aesthetic change, but it could have been much worse.

    0 讨论(0)
  • 2020-12-13 07:58
    - (void)viewDidLoad {
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    }
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    }
    
    - (void)keyboardWillShow:(NSNotification *)notification {
        [self performSelector:@selector(removeBar) withObject:nil afterDelay:0];
    }
    
    - (void)removeBar {
        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) {
                        [subviewWhichIsPossibleFormView removeFromSuperview];
                    }
                }
            }
        }
    }
    

    This works well.

    url: http://ios-blog.co.uk/iphone-development-tutorials/rich-text-editor-inserting-images-part-6/

    0 讨论(0)
  • 2020-12-13 07:58

    check out this one. https://gist.github.com/2048571. It works in iOS 5 and later, doesnt work for earlier versions.

    0 讨论(0)
  • 2020-12-13 08:00

    I was thinking of intercepting the UIKeyboardWillAppear notification, and giving it to a hidden text field instead, and forwarding the events through javascript to the real one in the webview. But it seems hairy. Things cursor movement and selection would then suck.

    0 讨论(0)
  • 2020-12-13 08:01

    It looks like there is a very simple way, but I'm pretty sure it will not pass the App Store review. Maybe someone has a clever idea? ;)

    @interface UIWebBrowserView : UIView
    @end
    
    @interface UIWebBrowserView (UIWebBrowserView_Additions)
    @end
    
    @implementation UIWebBrowserView (UIWebBrowserView_Additions)
    
    - (id)inputAccessoryView {
        return nil;
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-13 08:04

    This is an addition to Yun's answer. On iOS6 (6.0.1) there might be a horizontal grey border or shadow line on top of the row where the accessory (previous / next / done) used to be before it was removed. This fix works for me, and I'd like to share. Curious to hear if it works for you as well.

    To remove the border, I added this code to the inner loop of removeBar():

    if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIImageView"].location != NSNotFound) {
        [[subviewWhichIsPossibleFormView layer] setOpacity: 0.0];
    }
    

    We need to add the QuartzCore framework to the head of the .m file, so we can set the opacity of the layer involved.

    So, we get:

    ...
    
    #import <QuartzCore/QuartzCore.h>
    
    ...
    
    - (void)removeBar {
        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) {
                        [subviewWhichIsPossibleFormView removeFromSuperview];
                    }
                    // iOS 6 leaves a grey border / shadow above the hidden accessory row
                    if ([[subviewWhichIsPossibleFormView description] rangeOfString:@"UIImageView"].location != NSNotFound) {
                        // we need to add the QuartzCore framework for the next line
                        [[subviewWhichIsPossibleFormView layer] setOpacity: 0.0];
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题