UIWebView键盘的accessoryBar隐藏.docx
- 文档编号:7071077
- 上传时间:2023-01-16
- 格式:DOCX
- 页数:6
- 大小:16.63KB
UIWebView键盘的accessoryBar隐藏.docx
《UIWebView键盘的accessoryBar隐藏.docx》由会员分享,可在线阅读,更多相关《UIWebView键盘的accessoryBar隐藏.docx(6页珍藏版)》请在冰豆网上搜索。
UIWebView键盘的accessoryBar隐藏
UIWebView键盘的accessoryBar隐藏
当前越来越多的iOS应用开始使用html进行交互显示,却会发现在UIWebView弹出的键盘比UITextView的键盘多出了左右移动以及完成按钮,那么如何去掉这些我们并不需要的按钮呢?
苹果官方并没有给出方法,所以我们只能够自己来解决。
webview_keyboard.png
对于不同的iOS版本,我们的处理方法也会不同,因为官方没有提供方法,所有的方法都是需要自己去发现和修改。
这里只写出iOS7以上版本的方法:
funchideKeyBoard()->Void{
forwindowinUIApplication.sharedApplication().windows{
if!
window.isMemberOfClass(UIWindow.self){
letkeyboardWindow=window
if#available(iOS9.0,*){
self.removeAccessoryBarForiOS9(keyboardWindowasUIView)
}elseif#available(iOS8.0,*){
self.removeAccessoryBarForiOS8(keyboardWindowasUIView)
}else{
self.removeAccessoryBarForiOS7(keyboardWindowasUIView)
}
}
}
if#available(iOS9.0,*){
self.removeAccessoryBarForiOS9(UIApplication.sharedApplication().windows.last!
asUIView)
}
}
funcremoveAccessoryBarForiOS9(keyboardWindow:
UIView)->Void{
forpossibleFormView:
UIViewinkeyboardWindow.subviews{
ifpossibleFormView.isMemberOfClass(NSClassFromString("UIInputSetContainerView")!
){
forsubviewOfInputSetContainerViewinpossibleFormView.subviews{
ifsubviewOfInputSetContainerView.isMemberOfClass(NSClassFromString("UIInputSetHostView")!
){
forsubviewOfInputSetHostViewinsubviewOfInputSetContainerView.subviews{
//隐藏工具条NSClassFromString
ifsubviewOfInputSetHostView.isMemberOfClass(NSClassFromString("UIWebFormAccessory")!
){
subviewOfInputSetHostView.layer.opacity=0
subviewOfInputSetHostView.frame=CGRectZero
}elseif(subviewOfInputSetHostView.isMemberOfClass(NSClassFromString("_UIRemoteKeyboardPlaceholderView")!
)){
subviewOfInputSetHostView.layer.opacity=0
subviewOfInputSetHostView.frame=CGRectZero
//这里使用了私有方法获取对应的accessorBar,然后进行隐藏
varaccessory=subviewOfInputSetHostView.performSelector(Selector("placeheldView")).takeRetainedValue()
ifaccessory.isMemberOfClass(NSClassFromString("UIWebFormAccessory")!
){
letaccessory=accessoryas!
UIView
accessory.layer.opacity=0
accessory.frame=CGRectZero
}
}
//键盘背景,UIKBInputBackdropView有两个只隐藏上面的
elseifsubviewOfInputSetHostView.isMemberOfClass(NSClassFromString("UIKBInputBackdropView")!
)&&subviewOfInputSetHostView.frame.size.height<100{
subviewOfInputSetHostView.layer.opacity=0
subviewOfInputSetHostView.userInteractionEnabled=false
}
}
}
}
}
}
}
funcremoveAccessoryBarForiOS8(keyboardWindow:
UIView)->Void{
forpossibleFormView:
UIViewinkeyboardWindow.subviews{
ifpossibleFormView.isMemberOfClass(NSClassFromString("UIInputSetContainerView")!
){
forsubviewOfInputSetContainerViewinpossibleFormView.subviews{
ifsubviewOfInputSetContainerView.isMemberOfClass(NSClassFromString("UIInputSetHostView")!
){
forsubviewOfInputSetHostViewinsubviewOfInputSetContainerView.subviews{
//隐藏工具条
ifsubviewOfInputSetHostView.isMemberOfClass(NSClassFromString("UIWebFormAccessory")!
){
subviewOfInputSetHostView.layer.opacity=0
subviewOfInputSetHostView.frame=CGRectZero
}
//键盘背景,UIKBInputBackdropView有两个只隐藏上面的
elseifsubviewOfInputSetHostView.isMemberOfClass(NSClassFromString("UIKBInputBackdropView")!
)&&subviewOfInputSetHostView.frame.size.height<100{
subviewOfInputSetHostView.layer.opacity=0
subviewOfInputSetHostView.userInteractionEnabled=false
}
}
}
}
}
}
}
funcremoveAccessoryBarForiOS7(keyboardWindow:
UIView)->Void{
forpossibleFormView:
UIViewinkeyboardWindow.subviews{
ifpossibleFormView.isMemberOfClass(NSClassFromString("UIPeripheralHostView")!
){
forsubviewOfPeripheralHostViewinpossibleFormView.subviews{
//隐藏工具条
ifsubviewOfPeripheralHostView.isMemberOfClass(NSClassFromString("UIWebFormAccessory")!
){
subviewOfPeripheralHostView.layer.opacity=0
subviewOfPeripheralHostView.frame=CGRectZero
}
//键盘背景,UIKBInputBackdropView有两个只隐藏上面的
elseifsubviewOfPeripheralHostView.isMemberOfClass(NSClassFromString("UIKBInputBackdropView")!
)&&subviewOfPeripheralHostView.frame.size.height<100{
subviewOfPeripheralHostView.layer.opacity=0
subviewOfPeripheralHostView.userInteractionEnabled=false
}
}
}
}
}
实际中,隐藏键盘accessory后的样子如下图:
webview_keyboard_hideaccessory.png
需要注意的是,在示例代码使用设置layer透明,frame为空来进行隐藏,而不使用removeFromSuperView,是因为使用了removeFromSuperView,在键盘重新布局时会导致crash。
当然如果有朋友解决了crash,使用removeFromSuperView会更好。
附:
上面示例代码Github地址
使用removeFromSuperView产生crash的崩溃栈:
2016-07-0614:
10:
32.018WebViewKeyBoard[10846:
930540]Theviewhierarchyisnotpreparedfortheconstraint:
Whenaddedtoaview,theconstraint'sitemsmustbedescendantsofthatview(ortheviewitself).Thiswillcrashiftheconstraintneedstoberesolvedbeforetheviewhierarchyisassembled.Breakon-[UIView(UIConstraintBasedLayout)_viewHierarchyUnpreparedForConstraint:
]todebug.
2016-07-0614:
10:
32.018WebViewKeyBoard[10846:
930540]***Assertionfailurein-[UIInputSetHostView_layoutEngine_didAddLayoutConstraint:
roundingAdjustment:
mutuallyExclusiveConstraints:
],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/NSLayoutConstraint_UIKitAdditions.m:
590
2016-07-0614:
10:
32.025WebViewKeyBoard[10846:
930540]***Terminatingappduetouncaughtexception'NSInternalInconsistencyException',reason:
'Impossibletosetuplayoutwithviewhierarchyunpreparedforconstraint.'
***Firstthrowcallstack:
(
0CoreFoundation0x0000000105517d85__exceptionPreprocess+165
1libobjc.A.dylib0x00000001072bbdebobjc_exception_throw+48
2CoreFoundation0x0000000105517bea+[NSExceptionraise:
format:
arguments:
]+106
3Foundation0x0000000105968d5a-[NSAssertionHandlerhandleFailureInMethod:
object:
file:
lineNumber:
description:
]+198
4UIKit0x0000000106626b99__120-[UIView(UIConstraintBasedLayout)_layoutEngine_didAddLayoutConstraint:
roundingAdjustment:
mutuallyExclusiveConstraints:
]_block_invoke_2+254
5UIKit0x000000010662698b-[UIView(UIConstraintBasedLayout)_layoutEngine_didAddLayoutConstraint:
roundingAdjustment:
mutuallyExclusiveConstraints:
]+385
6UIKit0x0000000106626e04-[UIView(UIConstraintBasedLayout)_tryToAddConstraintWithoutUpdatingConstraintsArray:
roundingAdjustment:
mutuallyExclusiveConstraints:
]+65
7UIKit0x0000000106626f7d-[UIView(UIConstraintBasedLayout)_tryToAddConstraint:
roundingAdjustment:
mutuallyExclusiveConstraints:
]+288
8UIKit0x000000010662719f-[UIView(UIConstraintBasedLayout)_addConstraint:
]+274
9UIKit0x0000000106627438__50-[UIView(UIConstraintBasedLayout)addConstraints:
]_block_invoke+197
10Foundation0x00000001058f23d3-[NSISEnginewithBehaviors:
performModifications:
]+155
11UIKit0x0000000106626577-[UIView(UIConstraintBasedLayout)_withAutomaticEngineOptimizationDisabled:
]+58
12UIKit0x0000000106627348-[UIView(UIConstraintBasedLayout)addConstraints:
]+379
13UIKit0x00000001066b5531-[UIInputWindowControllerupdateViewConstraints]+3558
14UIKit0x00000001066b1fde-[UIInputSetHostView_didChangeKeyplaneWithContext:
]+224
15UIKit0x000000010650f1cc-[_UIKBCompatInputView_didChangeKeyplaneWithContext:
]+87
16UIKit0x0000000106004397-[UIKeyboard_didChangeKeyplaneWithContext:
]+324
17UIKit0x0000000105fe7b04-[UIKeyboardImpl_didChangeKeyplaneWithContext:
]+1100
18UIKit0x000000010620d5a3-[UIKeyboardLayoutStar(UIKeyboardLayoutJapanese50OnFlick)_didChangeKeyplaneWithContext:
]+183
19UIKit0x00000001061f121e-[UIKeyboardLayoutStarsetKeyplaneName:
]+4512
20UIKit0x0000000106209c37-[UIKeyboardLayoutStarsetShift:
]+158
21UIKit0x0000000105fec46a-[UIKeyboardImplnotifyShiftState]+73
22CoreFoundation0x000000010543cc37__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__+23
23CoreFoundation0x000000010543cba7__CFRunLoopDoObservers+391
24CoreFoundation0x00000001054326c4__CFRunLoopRun+836
25CoreFoundation0x00000001054320f8CFRunLoopRunSpecific+488
26GraphicsServices0x0000000109badad2GSEventRunModal+161
27UIKit0x0000000105d3ff09UIApplicationMain+171
28WebViewKeyBoard0x000000010532a242main+114
29libdyld.dylib0x0000000107d7f92dstart+1
30?
?
?
0x00000000000000010x0+1
)
libc++abi.dylib:
terminatingwithuncaughtexceptionoftypeNSException
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- UIWebView 键盘 accessoryBar 隐藏
![提示](https://static.bdocx.com/images/bang_tan.gif)