Ziank的技术博客

UIWebView键盘的accessoryBar隐藏

当前越来越多的iOS应用开始使用html进行交互显示,却会发现在UIWebView弹出的键盘比UITextView的键盘多出了左右移动以及完成按钮,那么如何去掉这些我们并不需要的按钮呢?苹果官方并没有给出方法,所以我们只能够自己来解决。

webview键盘图片

对于不同的iOS版本,我们的处理方法也会不同,因为官方没有提供方法,所有的方法都是需要自己去发现和修改。这里只写出iOS7以上版本的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
// 这里使用了私有方法获取对应的accessorBar,然后进行隐藏
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
}
}
}
}
}

实际中,隐藏键盘accessory后的样子如下图:

隐藏accessory的界面

需要注意的是,在示例代码使用设置layer透明,frame为空来进行隐藏,而不使用removeFromSuperView,是因为使用了removeFromSuperView,在键盘重新布局时会导致crash。当然如果有朋友解决了crash,使用removeFromSuperView会更好。

下面贴上crash的崩溃栈:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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