2010-01-20 62 views
16

我想擺脫當你在webview中關注文本字段時出現的鍵盤頂部的欄。我們還有其他一些處理方式,這是多餘的和不必要的。UIWebView鍵盤 - 擺脫「上一頁/下一頁/完成」欄

webview keyboard bar http://beautifulpixel.com/assets/iPhone_Simulator-20100120-152330.png

如果碰到這個問題,確保頭部到https://bugreport.apple.com和複製rdar:// 9844216

+0

嘿,你最終有幸運氣嗎?這個酒吧很煩人.. – marklar 2012-03-31 00:31:12

+1

@marklar是啊,不使用UIWebView ... :( – 2012-09-20 21:32:28

回答

0

不容易。你可以試着去瀏覽網頁視圖中的子視圖,但它會與蘋果禁忌。

如何不把網頁上的文本字段放在網頁上,並明確地將你的textfield/textview添加到webview中,以便它根本不顯示導航欄,並且你可以從頭開始添加你自己的?

1

我想攔截UIKeyboardWillAppear通知,並將其通知給隱藏文本字段,並通過javascript將事件轉發給webview中的真實通知。但它似乎毛茸茸的。光標移動和選擇的東西就會吮吸。

0
[[NSNotificationCenter defaultCenter] addObserver:self 

             selector:@selector(keyboardWasShown:) 

              name:UIKeyboardDidShowNotification object:nil]; 
-(void)keyboardWasShown:(NSNotification*)aNotification 
{ 
UIWindow* tempWindow; 

    //Because we cant get access to the UIKeyboard throught the SDK we will just use UIView. 
    //UIKeyboard is a subclass of UIView anyways 
    UIView* keyboard; 

    //Check each window in our application 
    for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; C++) 
    { 
      //Get a reference of the current window 
      tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c]; 

      //Get a reference of the current view 
      for(int i = 0; i < [tempWindow.subviews count]; i++) 
      { 
        keyboard = [tempWindow.subviews objectAtIndex:i]; 

        if([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) 
        {  
      keyboard.hidden = YES; 
      UIView* keyboardLayer; 
      for(int n = 0; n < [keyboard.subviews count]; n++) 
      { 
       keyboardLayer = [keyboard.subviews objectAtIndex:n]; 
       NSLog(@" keyboardLayer ::: %@ " ,keyboardLayer); 
       if([[keyboardLayer description] hasPrefix:@"<UIWebFormAccessory"] == YES) 
       { 
        [keyboardLayer removeFromSuperview ]; 
       } 
      } 
      keyboard.hidden = NO; 

        } 
      } 
    } 

NSLog(@"keyboardWasShown"); 

} 

檢查這個問題,以及:http://pastebin.com/s3Fkxvsk

10
- (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]; 
       } 
      } 
     } 
    } 
} 

這種運作良好。

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

+0

有沒有人得到這個實際工作?沒有爲我工作..? – marklar 2012-03-31 00:30:48

+1

它確實隱藏酒吧,但它留下了一個空白區域在那裏你是如何修復它的? – jAckOdE 2012-05-02 09:03:56

+1

謝謝。但是這個代碼不能用於UIWebView。在頁面按鈕的空白處出現 – Dmitry 2012-08-09 09:21:45

1

檢查出這一個。 https://gist.github.com/2048571。 它適用於iOS 5及更高版本,不適用於早期版本。

+0

你認爲你可以修改的要點,以便您可以簡單地添加一個按鈕到工具欄? – Hackmodford 2013-07-07 02:43:30

4

看起來有一個非常簡單的方法,但我很確定它不會通過App Store審查。也許有人有一個聰明的想法? ;)

@interface UIWebBrowserView : UIView 
@end 

@interface UIWebBrowserView (UIWebBrowserView_Additions) 
@end 

@implementation UIWebBrowserView (UIWebBrowserView_Additions) 

- (id)inputAccessoryView { 
    return nil; 
} 

@end 
+0

這就是聰明,儘管我非常懷疑蘋果可以。 – 2012-05-31 19:13:46

+1

雖然此方法成功移除表單助理,但我試圖將其提交給App Store,Apple因非公開API使用而立即拒絕。 – 2017-09-23 16:41:17

+0

感謝您的反饋@SachinRekhi! – Holtwick 2017-09-24 18:46:14

5

這是對雲的回答的補充。在iOS6(6.0.1)上,附件(上一個/下一個/完成)在刪除之前可能會出現水平灰色邊框或陰影線。此修補程序適用於我,我想分享。好奇聽到它是否也適合你。

要刪除邊框,我添加了這個代碼removeBar()的內部循環:

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

我們需要將QuartzCore框架添加到.m文件的頭部,所以我們可以設置所涉及的層的不透明度。

所以,我們得到:

... 

#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

完美!非常感謝,小細線讓我瘋狂! – Karoh 2013-02-12 01:08:51

+0

謝謝 - 擺脫了那條灰線。 – greentor 2013-02-14 11:26:50

+0

Perfect.Many謝謝! – Mateus 2013-03-20 16:34:58

2

有這樣做沒有公共的API。您可以通過檢查視圖層次結構並按照某些人的建議刪除視圖來刪除它,但這會非常危險。

這也是爲什麼這是一個壞主意:

如果蘋果沒有去除酒吧官方的API,他們可能有這樣做的很好的理由,和自己的代碼依賴於它在那裏。您可能從未遇到過問題,因爲您在英語鍵盤上執行了所有測試(例如)。但是如果您要刪除的視圖需要以其他語言輸入,或者爲了便於訪問,該怎麼辦?或者如果在iOS的未來版本中他們自己的實現發生了變化,以至於它認爲視圖始終存在?你的代碼會崩潰,你會被困在爭取更新的同時讓用戶等待幾個星期。

有趣的是,Remco的附加答案證明了這一點。在iOS 6.0.1上,進行了一項修改,要求對黑客進行修復。任何已經實施了ios 5入侵的人都會因此而被迫進行更新。幸運的是,這只是一種美學上的改變,但情況可能會更糟糕。

+0

在一行中:「不要f ****與私人API」:) – 2015-05-11 09:58:19

1

此代碼definetly適合我...希望這也適用於你。

- (void)viewDidLoad{ 
    [super viewDidLoad]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
} 


-(void)viewWillAppear:(BOOL)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 { 
    // Locate non-UIWindow. 
    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:@"<UIPeripheralHostView"]) { 
      for (UIView* peripheralView in [possibleFormView subviews]) { 

       // hides the backdrop (iOS 7) 
       if ([[peripheralView description] hasPrefix:@"<UIKBInputBackdropView"]) { 
        //skip the keyboard background....hide only the toolbar background 
        if ([peripheralView frame].origin.y == 0){ 
         [[peripheralView layer] setOpacity:0.0]; 
        } 
       } 
       // hides the accessory bar 
       if ([[peripheralView description] hasPrefix:@"<UIWebFormAccessory"]) { 
        // remove the extra scroll space for the form accessory bar 
        UIScrollView *webScroll; 
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) { 
         webScroll = [[self webviewpot] scrollView]; 
        } else { 
         webScroll = [[[self webviewpot] subviews] lastObject]; 
        } 
        CGRect newFrame = webScroll.frame; 
        newFrame.size.height += peripheralView.frame.size.height; 
        webScroll.frame = newFrame; 

        // remove the form accessory bar 
        [peripheralView removeFromSuperview]; 
       } 
       // hides the thin grey line used to adorn the bar (iOS 6) 
       if ([[peripheralView description] hasPrefix:@"<UIImageView"]) { 
        [[peripheralView layer] setOpacity:0.0]; 
       } 
      } 
     } 
    } 
} 
+0

這對我也適用:) – yageek 2014-06-27 20:17:20

相關問題