如果在關(guān)閉鍵盤同時(shí)彈出UIAlertView,會(huì)導(dǎo)致當(dāng)UIAlertView被dismiss時(shí),出現(xiàn)鍵盤閃動(dòng)一次(彈出又關(guān)閉)。
做某需求時(shí)被這個(gè)問題困擾好幾天,在stackoverflow上查詢無果;各種google網(wǎng)絡(luò)尋找前人經(jīng)驗(yàn),只有人說用dispatch_after做少量延時(shí)后再彈出alertView,像下面這樣。但是經(jīng)過測(cè)試效果不好,而且延時(shí)多少不好把握,容易出現(xiàn)UI不連貫。
解法一:
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC));
dispatch_after(delay, dispatch_get_main_queue(), ^{
[alertView show];
});
解法二:
如果用UIAlertController代替UIAlertView,就不會(huì)有任何UI問題。但是UIAlertController必須要通過某個(gè)VC進(jìn)行present操作,在別的模塊需要彈框時(shí),只能全局查詢頂層VC,比較麻煩,沒有UIAlertView用起來方便。
終極解法:
最后發(fā)現(xiàn)原因是鍵盤關(guān)閉動(dòng)畫被打斷后,UIAlertView所在window被系統(tǒng)設(shè)置成editing狀態(tài)。通過以下方式可完美解決,實(shí)現(xiàn)UIAlertViewDelegate的方法。
#pragma mark - UIAlertViewDelegate
- (void)didPresentAlertView:(UIAlertView *)alertView {
// Fix bug: show keyboard then dismiss after we have dismissed some alertview
[alertView.window endEditing:YES];
}