UIWebView鍵盤的accessoryBar隱藏

當(dāng)前越來越多的iOS應(yīng)用開始使用html進(jìn)行交互顯示,卻會發(fā)現(xiàn)在UIWebView彈出的鍵盤比UITextView的鍵盤多出了左右移動以及完成按鈕高氮,那么如何去掉這些我們并不需要的按鈕呢?蘋果官方并沒有給出方法腾啥,所以我們只能夠自己來解決芋类。

webview_keyboard.png

對于不同的iOS版本霜旧,我們的處理方法也會不同错忱,因?yàn)楣俜經(jīng)]有提供方法,所有的方法都是需要自己去發(fā)現(xiàn)和修改颁糟。這里只寫出iOS7以上版本的方法:

func hideKeyBoard() -> Void {
        for window in UIApplication.sharedApplication().windows {
            if !window.isMemberOfClass(UIWindow.self) {
                let keyboardWindow = window
                if #available(iOS 9.0, *) {
                    self.removeAccessoryBarForiOS9(keyboardWindow as UIView)
                } else if #available(iOS 8.0, *) {
                    self.removeAccessoryBarForiOS8(keyboardWindow as UIView)
                } else {
                    self.removeAccessoryBarForiOS7(keyboardWindow as UIView)
                }
            }
        }
        if #available(iOS 9.0, *) {
            self.removeAccessoryBarForiOS9(UIApplication.sharedApplication().windows.last! as UIView)
        }
    }
    
    func removeAccessoryBarForiOS9(keyboardWindow:UIView) -> Void {
        for possibleFormView:UIView in keyboardWindow.subviews {
            if possibleFormView.isMemberOfClass(NSClassFromString("UIInputSetContainerView")!) {
                for subviewOfInputSetContainerView in possibleFormView.subviews {
                    if subviewOfInputSetContainerView.isMemberOfClass(NSClassFromString("UIInputSetHostView")!) {
                        for subviewOfInputSetHostView in subviewOfInputSetContainerView.subviews {
                            // 隱藏工具條NSClassFromString
                            if subviewOfInputSetHostView.isMemberOfClass(NSClassFromString("UIWebFormAccessory")!) {
                                subviewOfInputSetHostView.layer.opacity = 0
                                subviewOfInputSetHostView.frame = CGRectZero
                            } else if (subviewOfInputSetHostView.isMemberOfClass(NSClassFromString("_UIRemoteKeyboardPlaceholderView")!)) {
                                subviewOfInputSetHostView.layer.opacity = 0
                                subviewOfInputSetHostView.frame = CGRectZero
                                
                                // 這里使用了私有方法獲取對應(yīng)的accessorBar航背,然后進(jìn)行隱藏
                                var accessory = subviewOfInputSetHostView.performSelector(Selector("placeheldView")).takeRetainedValue()
                                if accessory.isMemberOfClass(NSClassFromString("UIWebFormAccessory")!) {
                                    let accessory = accessory as! UIView
                                    accessory.layer.opacity = 0
                                    accessory.frame = CGRectZero
                                }
                                
                            }
                                // 鍵盤背景, UIKBInputBackdropView有兩個只隱藏上面的
                            else if subviewOfInputSetHostView.isMemberOfClass(NSClassFromString("UIKBInputBackdropView")!) && subviewOfInputSetHostView.frame.size.height < 100 {
                                subviewOfInputSetHostView.layer.opacity = 0
                                subviewOfInputSetHostView.userInteractionEnabled = false
                            }
                        }
                    }
                }
            }
        }
    }
    
    func removeAccessoryBarForiOS8(keyboardWindow:UIView) -> Void {
        for possibleFormView:UIView in keyboardWindow.subviews {
            if possibleFormView.isMemberOfClass(NSClassFromString("UIInputSetContainerView")!) {
                for subviewOfInputSetContainerView in possibleFormView.subviews {
                    if subviewOfInputSetContainerView.isMemberOfClass(NSClassFromString("UIInputSetHostView")!) {
                        for subviewOfInputSetHostView in subviewOfInputSetContainerView.subviews {
                            // 隱藏工具條
                            if subviewOfInputSetHostView.isMemberOfClass(NSClassFromString("UIWebFormAccessory")!) {
                                subviewOfInputSetHostView.layer.opacity = 0
                                subviewOfInputSetHostView.frame = CGRectZero
                            }
                                // 鍵盤背景, UIKBInputBackdropView有兩個只隱藏上面的
                            else if subviewOfInputSetHostView.isMemberOfClass(NSClassFromString("UIKBInputBackdropView")!) && subviewOfInputSetHostView.frame.size.height < 100 {
                                subviewOfInputSetHostView.layer.opacity = 0
                                subviewOfInputSetHostView.userInteractionEnabled = false
                            }
                        }
                    }
                }
            }
        }
    }
    
    func removeAccessoryBarForiOS7(keyboardWindow:UIView) -> Void {
        for possibleFormView:UIView in keyboardWindow.subviews {
            if possibleFormView.isMemberOfClass(NSClassFromString("UIPeripheralHostView")!) {
                for subviewOfPeripheralHostView in possibleFormView.subviews {
                    // 隱藏工具條
                    if subviewOfPeripheralHostView.isMemberOfClass(NSClassFromString("UIWebFormAccessory")!) {
                        subviewOfPeripheralHostView.layer.opacity = 0
                        subviewOfPeripheralHostView.frame = CGRectZero
                    }
                    // 鍵盤背景, UIKBInputBackdropView有兩個只隱藏上面的
                    else if subviewOfPeripheralHostView.isMemberOfClass(NSClassFromString("UIKBInputBackdropView")!) && subviewOfPeripheralHostView.frame.size.height < 100 {
                        subviewOfPeripheralHostView.layer.opacity = 0
                        subviewOfPeripheralHostView.userInteractionEnabled = false
                    }
                }
            }
        }
    }

實(shí)際中喉悴,隱藏鍵盤accessory后的樣子如下圖:

webview_keyboard_hideaccessory.png

需要注意的是棱貌,在示例代碼使用設(shè)置layer透明,frame為空來進(jìn)行隱藏箕肃,而不使用removeFromSuperView婚脱,是因?yàn)槭褂昧?code>removeFromSuperView,在鍵盤重新布局時會導(dǎo)致crash勺像。當(dāng)然如果有朋友解決了crash障贸,使用removeFromSuperView會更好。

附:

  1. 上面示例代碼Github地址
  2. 使用removeFromSuperView產(chǎn)生crash的崩潰棧:
2016-07-06 14:10:32.018 WebViewKeyBoard[10846:930540] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7f82a172d880 V:[_UIRemoteKeyboardPlaceholderView:0x7f82a14e7e30]-(0)-[_UIKBCompatInputView:0x7f82a16f9950]>
 When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
2016-07-06 14:10:32.018 WebViewKeyBoard[10846:930540] *** Assertion failure in -[UIInputSetHostView _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/NSLayoutConstraint_UIKitAdditions.m:590
2016-07-06 14:10:32.025 WebViewKeyBoard[10846:930540] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Impossible to set up layout with view hierarchy unprepared for constraint.'
*** First throw call stack:
(
 0   CoreFoundation                      0x0000000105517d85 __exceptionPreprocess + 165
 1   libobjc.A.dylib                     0x00000001072bbdeb objc_exception_throw + 48
 2   CoreFoundation                      0x0000000105517bea +[NSException raise:format:arguments:] + 106
 3   Foundation                          0x0000000105968d5a -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
 4   UIKit                               0x0000000106626b99 __120-[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:]_block_invoke_2 + 254
 5   UIKit                               0x000000010662698b -[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 385
 6   UIKit                               0x0000000106626e04 -[UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutuallyExclusiveConstraints:] + 65
 7   UIKit                               0x0000000106626f7d -[UIView(UIConstraintBasedLayout) _tryToAddConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 288
 8   UIKit                               0x000000010662719f -[UIView(UIConstraintBasedLayout) _addConstraint:] + 274
 9   UIKit                               0x0000000106627438 __50-[UIView(UIConstraintBasedLayout) addConstraints:]_block_invoke + 197
 10  Foundation                          0x00000001058f23d3 -[NSISEngine withBehaviors:performModifications:] + 155
 11  UIKit                               0x0000000106626577 -[UIView(UIConstraintBasedLayout) _withAutomaticEngineOptimizationDisabled:] + 58
 12  UIKit                               0x0000000106627348 -[UIView(UIConstraintBasedLayout) addConstraints:] + 379
 13  UIKit                               0x00000001066b5531 -[UIInputWindowController updateViewConstraints] + 3558
 14  UIKit                               0x00000001066b1fde -[UIInputSetHostView _didChangeKeyplaneWithContext:] + 224
 15  UIKit                               0x000000010650f1cc -[_UIKBCompatInputView _didChangeKeyplaneWithContext:] + 87
 16  UIKit                               0x0000000106004397 -[UIKeyboard _didChangeKeyplaneWithContext:] + 324
 17  UIKit                               0x0000000105fe7b04 -[UIKeyboardImpl _didChangeKeyplaneWithContext:] + 1100
 18  UIKit                               0x000000010620d5a3 -[UIKeyboardLayoutStar(UIKeyboardLayoutJapanese50OnFlick) _didChangeKeyplaneWithContext:] + 183
 19  UIKit                               0x00000001061f121e -[UIKeyboardLayoutStar setKeyplaneName:] + 4512
 20  UIKit                               0x0000000106209c37 -[UIKeyboardLayoutStar setShift:] + 158
 21  UIKit                               0x0000000105fec46a -[UIKeyboardImpl notifyShiftState] + 73
 22  CoreFoundation                      0x000000010543cc37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
 23  CoreFoundation                      0x000000010543cba7 __CFRunLoopDoObservers + 391
 24  CoreFoundation                      0x00000001054326c4 __CFRunLoopRun + 836
 25  CoreFoundation                      0x00000001054320f8 CFRunLoopRunSpecific + 488
 26  GraphicsServices                    0x0000000109badad2 GSEventRunModal + 161
 27  UIKit                               0x0000000105d3ff09 UIApplicationMain + 171
 28  WebViewKeyBoard                     0x000000010532a242 main + 114
 29  libdyld.dylib                       0x0000000107d7f92d start + 1
 30  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吟宦,一起剝皮案震驚了整個濱河市篮洁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌殃姓,老刑警劉巖袁波,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蜗侈,居然都是意外死亡篷牌,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進(jìn)店門踏幻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來枷颊,“玉大人,你說我怎么就攤上這事∝裁纾” “怎么了信卡?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長听诸。 經(jīng)常有香客問我坐求,道長,這世上最難降的妖魔是什么晌梨? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任桥嗤,我火速辦了婚禮,結(jié)果婚禮上仔蝌,老公的妹妹穿的比我還像新娘泛领。我一直安慰自己,他們只是感情好敛惊,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布渊鞋。 她就那樣靜靜地躺著,像睡著了一般瞧挤。 火紅的嫁衣襯著肌膚如雪锡宋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天特恬,我揣著相機(jī)與錄音执俩,去河邊找鬼。 笑死癌刽,一個胖子當(dāng)著我的面吹牛役首,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播显拜,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼衡奥,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了远荠?” 一聲冷哼從身側(cè)響起矮固,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎譬淳,沒想到半個月后档址,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡瘦赫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年辰晕,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片确虱。...
    茶點(diǎn)故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡含友,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情窘问,我是刑警寧澤辆童,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站惠赫,受9級特大地震影響把鉴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜儿咱,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一庭砍、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧混埠,春花似錦怠缸、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至吏颖,卻和暖如春搔体,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背半醉。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工疚俱, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人奉呛。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓计螺,卻偏偏與公主長得像夯尽,于是被迫代替她去往敵國和親瞧壮。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫匙握、插件咆槽、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,104評論 4 62
  • 截止到2014年11月底,我第二次加入SGS (一家在瑞士蘇黎士上市圈纺、股價達(dá)每股2000多法朗的全球最大的第三方檢...
    格思阿甘閱讀 2,265評論 21 10
  • 這是一個性向測試題秦忿。 如果你在ATM機(jī)上取款一千元,結(jié)果取款機(jī)卻吐出來2000元蛾娶。大致來說有幾種處理方式灯谣,甲會取走...
    楚天闊闊閱讀 258評論 0 0
  • 姓名:冉喬琪~公司:天興醫(yī)藥 【日精進(jìn)打卡第※113※天】 【知~學(xué)習(xí)】 《六項(xiàng)精進(jìn)》2遍 共335遍 《大學(xué)》2...
    小小新醬閱讀 165評論 0 0