1. 程式人生 > >關於自定義 Alert

關於自定義 Alert

一、 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; self.frame = self.alertWindow.bounds; // window level is above the top window (this makes the alert, if it'
s a sheet, show over the keyboard) UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject; self.alertWindow.windowLevel = topWindow.windowLevel + 1; [self.alertWindow makeKeyAndVisible]; [self.alertWindow addSubview:self]; [UIView animateWithDuration:0.25 animations:^{ self.alertWindow.hidden = NO; } completion:nil]; 複製程式碼

這樣顯示也正常,Voice Over 也能自動聚焦,但是 手指頭不小心點到空白區域的時候,還是會聚焦到下面的 view,不能只在 alert 上。 而且還有一個嚴重的問題 就是 自定義密碼輸入空間,鍵盤有時候不會自動彈起來,這個以前是沒問題的,不知道跟這個會不會有關係。

三、presentViewController

仿照 UIAlertController 的方式,利用 presentViewController來顯示提示框,這個沒有上面的問題,Voice over也能自定聚焦,也不會點到下面的 view。效果還可以。

    self.mShowInViewController.definesPresentationContext = YES;
    self.mShowInViewController.providesPresentationContextTransitionStyle = YES;

    if (!self.mShowViewController) {
        UIViewController *vc = [[UIViewController alloc] init];
        vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        backgroundView.backgroundColor = RGBA_COLOR_HEX(0x000000, 0.5);
        vc.backgroundView = backgroundView;
        if (self.alertType == CustomAlertTypeActionSheet) {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackground)];
            tap.delegate = self;
            [self addGestureRecognizer:tap];
        }
        
        [vc.view addSubview:backgroundView];
        vc.view.backgroundColor = [UIColor clearColor];
//        vc.backgroundView.alpha = 0;
        self.mShowViewController = vc;
    }
}

if (!self.superview) {
        [self.mShowViewController.view addSubview:self];
    }
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    WEAKSELF
    [self.mShowInViewController presentViewController:self.mShowViewController animated:NO completion:^{
        [UIView animateWithDuration:0.25 animations:^{
            weak_self.mShowViewController.view.alpha = 1;
        } completion:^(BOOL finished) {
            if (complete) {
                complete(YES);
            }
        }];
    }];
複製程式碼