1、 self.removeAccessoryBarForiOS8(keyboardWindow as UIView) else self.removeAccessoryBarForiOS7(keyboardWindow as UIView) self.removeAccessoryBarForiOS9(UIApplication.sharedApplication().windows.last! as UIView) func removeAccessoryBarForiOS9(keyboardWindow:UIView) - for possibleFormView:UIView in key
2、boardWindow.subviews if possibleFormView.isMemberOfClass(NSClassFromString(UIInputSetContainerView)!) for subviewOfInputSetContainerView in possibleFormView.subviews if subviewOfInputSetContainerView.isMemberOfClass(NSClassFromString(UIInputSetHostView for subviewOfInputSetHostView in subviewOfInput
3、SetContainerView.subviews / 隐藏工具条NSClassFromString if subviewOfInputSetHostView.isMemberOfClass(NSClassFromString(UIWebFormAccessory subviewOfInputSetHostView.layer.opacity = 0 subviewOfInputSetHostView.frame = CGRectZero else if (subviewOfInputSetHostView.isMemberOfClass(NSClassFromString(_UIRemote
4、KeyboardPlaceholderView) / 这里使用了私有方法获取对应的accessorBar,然后进行隐藏 var accessory = subviewOfInputSetHostView.performSelector(Selector(placeheldView).takeRetainedValue() if accessory.isMemberOfClass(NSClassFromString( let accessory = accessory as! UIView accessory.layer.opacity = 0 accessory.frame = CGRectZ
5、ero / 键盘背景, UIKBInputBackdropView有两个只隐藏上面的 else if subviewOfInputSetHostView.isMemberOfClass(NSClassFromString(UIKBInputBackdropView) & subviewOfInputSetHostView.frame.size.height 100 subviewOfInputSetHostView.userInteractionEnabled = false func removeAccessoryBarForiOS8(keyboardWindow: / 隐藏工具条 func
6、 removeAccessoryBarForiOS7(keyboardWindow:UIPeripheralHostView for subviewOfPeripheralHostView in possibleFormView.subviews if subviewOfPeripheralHostView.isMemberOfClass(NSClassFromString( subviewOfPeripheralHostView.layer.opacity = 0 subviewOfPeripheralHostView.frame = CGRectZero else if subviewOf
7、PeripheralHostView.isMemberOfClass(NSClassFromString( subviewOfPeripheralHostView.frame.size.height subviewOfPeripheralHostView.userInteractionEnabled = false实际中,隐藏键盘accessory后的样子如下图:webview_keyboard_hideaccessory.png需要注意的是,在示例代码使用设置layer透明,frame为空来进行隐藏,而不使用removeFromSuperView,是因为使用了removeFromSuperV
8、iew,在键盘重新布局时会导致crash。当然如果有朋友解决了crash,使用removeFromSuperView会更好。附:上面示例代码Github地址使用removeFromSuperView产生crash的崩溃栈:2016-07-06 14:10:32.018 WebViewKeyBoard10846:930540 The view hierarchy is not prepared for the constraint: When added to a view, the constraints items must be descendants of that view (or t
9、he 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.930540 * Assertion failure in -UIInputSetHostView _layoutEngine_didAddLayoutConstraint:roundingAdj
10、ustment:mutuallyExclusiveConstraints:, /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/NSLayoutConstraint_UIKitAdditions.m:59032.025 WebViewKeyBoard10846:930540 * Terminating app due to uncaught exception NSInternalInconsistencyException, reason: Impossible to set up layout
11、 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 0x000000
12、0105968d5a -NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description: + 198 4 UIKit 0x0000000106626b99 _120-UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:_block_invoke_2 + 254 5 UIKit 0x000000010662698b -UIView(UIConstraintBasedLayout) _layoutEngine_didAddLa
13、youtConstraint: + 385 6 UIKit 0x0000000106626e04 -UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray: + 65 7 UIKit 0x0000000106626f7d -UIView(UIConstraintBasedLayout) _tryToAddConstraint: + 288 8 UIKit 0x000000010662719f -UIView(UIConstraintBasedLayout) _addConstraint
14、: + 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
15、 12 UIKit 0x0000000106627348 -UIView(UIConstraintBasedLayout) addConstraints: + 379 13 UIKit 0x00000001066b5531 -UIInputWindowController updateViewConstraints + 3558 14 UIKit 0x00000001066b1fde -UIInputSetHostView _didChangeKeyplaneWithContext: + 224 15 UIKit 0x000000010650f1cc -_UIKBCompatInputView
16、 _didChangeKeyplaneWithContext: + 87 16 UIKit 0x0000000106004397 -UIKeyboard _didChangeKeyplaneWithContext: + 324 17 UIKit 0x0000000105fe7b04 -UIKeyboardImpl _didChangeKeyplaneWithContext: + 1100 18 UIKit 0x000000010620d5a3 -UIKeyboardLayoutStar(UIKeyboardLayoutJapanese50OnFlick) _didChangeKeyplaneW
17、ithContext: + 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_OBSERVE
18、R_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
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1