ios11在键盘弹起和UIAlertController出现之后会出现一个UITextEffectsWindow且不消失了,内部包含UIInputSetContainerView->UIInputSetHostView(高度:258),SVProgressHuB为了适配键盘弹起,会遍历window获取这个window及其中UIInputSetHostView的高度以调整hudView的center,这样就会造成huvView之后一直不居中。
那么我们应该怎么解决呢?我是用的办法是:判断UIInputSetHostView是否在window中,如果在则在调起SVProgressHUD提示框时,设置一下其相对于中心点的偏移量:
#define SHOWSUCCESS_SVP_STRING(str) dispatch_async(dispatch_get_main_queue(),^{\
[SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];\
[SVProgressHUD setSuccessImage:nil];\
[SVProgressHUD showSuccessWithStatus:str];\
[SVProgressHUD dismissWithDelay:1.5f];\
if ([CommonTools adjustSVProgressHuDHudViewCenter] > 0){\
[SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, (-CGRectGetHeight(UIApplication.sharedApplication.statusBarFrame) + [CommonTools adjustSVProgressHuDHudViewCenter]) * 0.45)];\
}\
});
判断及获取UIInputSetHostView高度的代码<同SVProgressHUD内部的代码,SVProgressHUD.m文件 1328行>:
+(CGFloat)adjustSVProgressHuDHudViewCenter{
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if(![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
for (__strong UIView *possibleKeyboard in [keyboardWindow subviews]) {
if([possibleKeyboard isKindOfClass:NSClassFromString(@"UIPeripheralHostView")] || [possibleKeyboard isKindOfClass:NSClassFromString(@"UIKeyboard")]) {
return CGRectGetHeight(possibleKeyboard.bounds);
} else if([possibleKeyboard isKindOfClass:NSClassFromString(@"UIInputSetContainerView")]) {
for (__strong UIView *possibleKeyboardSubview in [possibleKeyboard subviews]) {
if([possibleKeyboardSubview isKindOfClass:NSClassFromString(@"UIInputSetHostView")]) {
return CGRectGetHeight(possibleKeyboardSubview.bounds);
}
}
}
}
return 0;
}