iOS去除Webview鍵盤頂部工具欄的方法
前言
在默認(rèn)情況下,iOS 使用 Webview 打開的網(wǎng)頁,在進(jìn)行表單輸入時(shí),彈出的鍵盤頂部會(huì)多出一個(gè)工具欄。
左邊有兩個(gè)上下按鈕,右邊有一個(gè)Done/完成按鈕。這是用來切換輸入框的,就像 PC 上按Tab鍵可以切換輸入框一樣。
為了讓 App 中嵌入的 H5 更接近 Native,咱們可以去掉它。
UIWebView
UIWebView,可以使用[self hideKeyboardShortcutBar:self.webView]去掉工具欄。
- (void) hideKeyboardShortcutBar: (UIView *)view
{
for (UIView *sub in view.subviews) {
[self hideKeyboardShortcutBar:sub];
if ([NSStringFromClass([sub class]) isEqualToString:@"UIWebBrowserView"]) {
Method method = class_getInstanceMethod(sub.class, @selector(inputAccessoryView));
IMP newImp = imp_implementationWithBlock(^(id _s) {
if ([sub respondsToSelector:@selector(inputAssistantItem)]) {
UITextInputAssistantItem *inputAssistantItem = [sub inputAssistantItem];
inputAssistantItem.leadingBarButtonGroups = @[];
inputAssistantItem.trailingBarButtonGroups = @[];
}
return nil;
});
method_setImplementation(method, newImp);
}
}
}
WkWebView
WkWebView,可以使用[self hideWKWebviewKeyboardShortcutBar:self.webView]去掉工具欄。
// 步驟一:創(chuàng)建一個(gè) _NoInputAccessoryView
@interface _NoInputAccessoryView : NSObject
@end
@implementation _NoInputAccessoryView
- (id)inputAccessoryView {
return nil;
}
@end
// 步驟二:去掉 WkWebviewe Done 工具欄
- (void) hideWKWebviewKeyboardShortcutBar:(WKWebView *)webView {
UIView *targetView;
for (UIView *view in webView.scrollView.subviews) {
if([[view.class description] hasPrefix:@"WKContent"]) {
targetView = view;
}
}
if (!targetView) {
return;
}
NSString *noInputAccessoryViewClassName = [NSString stringWithFormat:@"%@_NoInputAccessoryView", targetView.class.superclass];
Class newClass = NSClassFromString(noInputAccessoryViewClassName);
if(newClass == nil) {
newClass = objc_allocateClassPair(targetView.class, [noInputAccessoryViewClassName cStringUsingEncoding:NSASCIIStringEncoding], 0);
if(!newClass) {
return;
}
Method method = class_getInstanceMethod([_NoInputAccessoryView class], @selector(inputAccessoryView));
class_addMethod(newClass, @selector(inputAccessoryView), method_getImplementation(method), method_getTypeEncoding(method));
objc_registerClassPair(newClass);
}
object_setClass(targetView, newClass);
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
通過UIKit坐標(biāo)系來全面掌握iOS中的UIScrollView組件
iOS開發(fā)套件中的UIScrollView組件十分強(qiáng)大,不僅是滾動(dòng),縮放操作也能夠控制自如,其核心當(dāng)然是坐標(biāo)軸上的控制,下面就通過UIKit坐標(biāo)系來全面掌握iOS中的UIScrollView組件2016-05-05
iOS DispatchSourceTimer 定時(shí)器的具體使用
定時(shí)器在很多地方都可以用到,本文主要介紹了iOS DispatchSourceTimer 定時(shí)器的具體使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
flutter狀態(tài)管理Provider的使用學(xué)習(xí)
這篇文章主要為大家介紹了flutter狀態(tài)管理Provider的使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
iOS高仿微信相冊(cè)界面翻轉(zhuǎn)過渡動(dòng)畫效果
在圖片界面點(diǎn)擊右下角的查看評(píng)論會(huì)翻轉(zhuǎn)到評(píng)論界面,評(píng)論界面點(diǎn)擊左上角的返回按鈕會(huì)反方向翻轉(zhuǎn)回圖片界面,真正的實(shí)現(xiàn)方法,與傳統(tǒng)的導(dǎo)航欄過渡其實(shí)只有一行代碼的區(qū)別,下面小編通過本文給大家介紹下ios高仿微信相冊(cè)界面翻轉(zhuǎn)過渡動(dòng)畫效果,一起看看吧2016-11-11
iOS 本地視頻和網(wǎng)絡(luò)視頻流播放實(shí)例代碼
本篇文章主要介紹了iOS 本地視頻和網(wǎng)絡(luò)視頻流播放實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
IOS 解決推送本地國際化 loc-key 本地化失敗的問題
本文主要介紹IOS 推送國際化問題,在開發(fā) IOS 項(xiàng)目過程中對(duì)軟件的國際化有的項(xiàng)目需求是需要的,這里給大家一個(gè)示例,有需要的小伙伴可以參考下2016-07-07

