前言
成都創(chuàng)新互聯(lián)公司專注于長島企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),電子商務(wù)商城網(wǎng)站建設(shè)。長島網(wǎng)站建設(shè)公司,為長島等地區(qū)提供建站服務(wù)。全流程按需求定制制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
大家都知道在iOS8中引入了UIAlertController,通過UIAlertController可以方便的添加文本框進(jìn)行編輯,但是,在輸入錯(cuò)誤的內(nèi)容時(shí),如何對用戶進(jìn)行提醒就成了問題,因?yàn)閁IAlertController中的所有UIAlertAction都會導(dǎo)致UIAlertController的消失。這里,我就描述兩種提示的方法,分別是晃動文本框和修改邊框的顏色。下面話不多說了,來一起看看詳細(xì)的實(shí)現(xiàn)方法吧。
晃動UITextField
晃動UITextField其實(shí)就是對它添加一個(gè)動畫效果,參考了Stack Overflow上的做法,通過添加position的動畫,可以實(shí)現(xiàn)UIAlertController中的UITextField的晃動效果。
- (void)shakeField:(UITextField *)textField { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; animation.duration = 0.07; animation.repeatCount = 4; animation.autoreverses = YES; animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(textField.centerX - 10, textField.centerY)]; animation.toValue = [NSValue valueWithCGPoint:CGPointMake(textField.centerX + 10, textField.centerY)]; [textField.layer addAnimation:animation forKey:@"position"]; }
修改UITextField的邊框顏色
UIAlertController中文本框的默認(rèn)邊框顏色都是黑色,通常在輸入異常時(shí)會改為紅色進(jìn)行提醒,這個(gè)時(shí)候,如果直接修改UITextField的border將會變成下圖樣式:
- (void)testAlert { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"測試" message:@"測試輸入框邊框顏色" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.layer.borderColor = [UIColor redColor].CGColor; textField.layer.borderWidth = 1; }]; [self presentViewController:alert animated:YES completion:nil]; }
而在實(shí)際中我們應(yīng)該這樣修改:
- (void)testAlert { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"測試" message:@"測試輸入框邊框顏色" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { self.currentField = textField; }]; [self presentViewController:alert animated:YES completion:^{ [[self.currentField superview] superview].backgroundColor = [UIColor redColor]; }]; }
這樣的產(chǎn)生效果才是我們想要的。
需要注意的是:一定要在present以后進(jìn)行設(shè)置,否則會發(fā)現(xiàn)設(shè)置是無效的,因?yàn)闆]有present之前,textField的superview是nil,設(shè)置是無效的。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,本文還有許多不足,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。
當(dāng)前名稱:iOSUIAlertController中UITextField添加晃動效果與邊框顏色詳解
網(wǎng)頁URL:http://www.ekvhdxd.cn/article36/jiehpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站維護(hù)、微信小程序、電子商務(wù)、網(wǎng)站設(shè)計(jì)、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)