Show UIAlertController over keyboard

前端 未结 4 1318
不知归路
不知归路 2020-12-25 14:48

In iOS 8 and lower show a UIActionSheet when keyboard is presented will present the action sheet over the keyboard. With iOS 9 this is no longer the case.

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-25 15:28

    The answer supplied by Leo is broken as of iOS 11, because Apple now prevents you from setting a windowLevel above 10000000. A fix is to implement a custom UIWindow and override the windowLevel receiver:

    @interface TopWindow : UIWindow @end
    
    @implementation TopWindow
    - (UIWindowLevel) windowLevel {
        return 20000000.000;
    }
    @end
    
    // usage:
    UIWindow* w = [[TopWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    w.rootViewController = [UIViewController new];
    w.hidden = NO;
    
    [w.rootViewController presentViewController:yourActionSheetController animated:YES completion:nil];
    

    This approach should be backwards compatible, but haven't tested all known versions. Happy hacking!

提交回复
热议问题