关于自定义 Alert

本文探讨了自定义Alert的实现方法,包括直接添加到window、创建独立window以及使用presentViewController。文章指出每种方法的优缺点,如窗口焦点、键盘弹出和交互问题,并提供了完整代码示例。最后建议结合Window和presentViewController来避免冲突和资源泄漏。
摘要由CSDN通过智能技术生成
一、 window add subview

自定义 alert 的时候,没有做统一规划,都是实现一个 view,然后添加到 window 上。例如:

UIView *alert = [[UIView alloc] initWithFrame:];
[window addSubView:alert];
复制代码

这样本来也没有什么问题,有一天需求来了要支持 Voice Over,问题来了,直接盖上去的 view 不会自动聚焦,也就是手指左右滑动的时候,不能选中这个 view。而且不能 alert 出来的时候,不能自动读出。

二、利用Window 的 makeKeyAndVisible

然后想起以前 UIAlertView 显示的时候是有一个单独 window 的,于是也仿照 这样,自己建一个 window,来显示 alert,

    self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.alertWindow.rootViewController = [[UIViewController alloc] init];

    id<UIApplicationDelegate> delegate = [UIApplication sharedApplication].delegate;
    // Applications that does not load with UIMainStoryboardFile might not have a window property:
    if ([delegate respondsToSelector:@selector(window)]) {
        // we inherit the main window's tintColor
        self.alertWindow.tintColor = delegate.window.tintColor;
    }
    self.alertWindow.hidden = YES;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值