国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS UIAlertController中UITextField添加晃動效果與邊框顏色詳解

2019-10-21 18:44:10
字體:
來源:轉載
供稿:網友

前言

大家都知道在iOS8中引入了UIAlertController,通過UIAlertController可以方便的添加文本框進行編輯,但是,在輸入錯誤的內容時,如何對用戶進行提醒就成了問題,因為UIAlertController中的所有UIAlertAction都會導致UIAlertController的消失。這里,我就描述兩種提示的方法,分別是晃動文本框和修改邊框的顏色。下面話不多說了,來一起看看詳細的實現方法吧。

晃動UITextField

晃動UITextField其實就是對它添加一個動畫效果,參考了Stack Overflow上的做法,通過添加position的動畫,可以實現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中文本框的默認邊框顏色都是黑色,通常在輸入異常時會改為紅色進行提醒,這個時候,如果直接修改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];}

ios,uitextfield,邊框,邊框顏色

而在實際中我們應該這樣修改:

- (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]; }];}

這樣的產生效果才是我們想要的。

ios,uitextfield,邊框,邊框顏色

需要注意的是:一定要在present以后進行設置,否則會發現設置是無效的,因為沒有present之前,textField的superview是nil,設置是無效的。

總結

以上就是這篇文章的全部內容了,本文還有許多不足,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肇庆市| 昌吉市| 吴忠市| 广宁县| 库伦旗| 镇雄县| 九龙坡区| 永嘉县| 安宁市| 高尔夫| 蓬安县| 科技| 东乌| 龙陵县| 惠安县| 晋中市| 永和县| 革吉县| 德州市| 深州市| 修武县| 岚皋县| 临江市| 天全县| 西乡县| 唐海县| 偃师市| 丹棱县| 将乐县| 南乐县| 东乡县| 厦门市| 五河县| 巨鹿县| 伊金霍洛旗| 田林县| 无为县| 张家界市| 丽江市| 澄江县| 台湾省|