前幾天驱闷,我寫(xiě)了一篇介紹如何自己動(dòng)手寫(xiě)一個(gè)自定義彈出框的文章(傳送門(mén):“自己動(dòng)手寫(xiě)一個(gè)自定義彈出框-MGPopController”)脚作,這篇算是續(xù)作吧碾盟,主要介紹下如何給彈出框添加UITextField輸入框德频,目前源碼和demo我已經(jīng)更新到github上(任意門(mén):MGPopController)
實(shí)現(xiàn)方式
我們都知道UIAlertController里是可以添加UITextField的路操,所以我按照UIAlertController的方式也設(shè)計(jì)了一個(gè)api:
- (void)addTextFieldWithConfiguration:(void (^)(UITextField *textField))configuration;
具體實(shí)現(xiàn):
- (void)addTextFieldWithConfiguration:(void (^)(UITextField *textField))configuration {
UITextField *textField = [[UITextField alloc] init];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:13.0f];
NSMutableArray *arr = [NSMutableArray arrayWithArray:_textFields];
[arr addObject:textField];
_textFields = arr;
if (configuration) {
configuration(textField);
}
}
添加了UITextField,具體怎么展示就是UI布局的問(wèn)題了诅诱,我說(shuō)過(guò)髓堪,UI不是重點(diǎn),你只要根據(jù)自己的需要做處理就好了娘荡。
使用方法
在Demo里我用一個(gè)登陸界面展示了具體的用法干旁,先看一下實(shí)現(xiàn)后的效果:
screenshot4.gif
具體代碼:
MGPopController *pop = [[MGPopController alloc] initWithTitle:@"登錄" message:nil image:nil];
[pop addAction:[MGPopAction actionWithTitle:@"關(guān)閉" action:^{
NSLog(@"關(guān)閉...");
[[IQKeyboardManager sharedManager] resignFirstResponder];
}]];
__weak __typeof(&*self)weakSelf = self;
MGPopAction *action = [MGPopAction actionWithTitle:@"登錄" action:^{
[[IQKeyboardManager sharedManager] resignFirstResponder];
[weakSelf loginWithMobile:[pop.textFields firstObject].text password:[pop.textFields objectAtIndex:1].text];
}];
action.autoDismiss = NO;
[pop addAction:action];
[pop addTextFieldWithConfiguration:^(UITextField *textField) {
textField.placeholder = @"請(qǐng)輸入手機(jī)號(hào)...";
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.delegate = self;
}];
[pop addTextFieldWithConfiguration:^(UITextField *textField) {
textField.placeholder = @"請(qǐng)輸入密碼...";
textField.secureTextEntry = YES;
}];
pop.titleFont = [UIFont boldSystemFontOfSize:17.0f];
pop.messageFont = [UIFont systemFontOfSize:13.0];
pop.showActionSeparator = YES;
pop.actionSpacing = 0;
pop.actionPaddingLeftRight = 0;
pop.actionPaddingBottom = 0;
pop.showCloseButton = NO;
[pop show];
self.loginController = pop;
幾點(diǎn)說(shuō)明
- 對(duì)UITextField可以指定delegate,這樣就可以使用UITextField的這些代理方法了炮沐,比如限制輸入框最大輸入字符:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSInteger length = textField.text.length - range.length + string.length;
if (length > 11) {
return NO;
}
return YES;
}
- 獲取UITextField中值的方法:
在MGPopController中已經(jīng)暴露了只讀的textFields屬性争群,可以通過(guò)這個(gè)屬性獲取所有添加的UITextField,然后就能獲得每個(gè)textField里的值了
@property (nonatomic, readonly) NSArray<UITextField *> *textFields;
- 如何做到點(diǎn)擊操作按鈕的時(shí)候不隱藏彈出框大年,而是在適當(dāng)?shù)臅r(shí)候消失换薄,比如登錄的時(shí)候玉雾,希望能在從服務(wù)端請(qǐng)求回來(lái)的時(shí)候再關(guān)閉輸入框
1.利用MGPopAction里的autoDimiss屬性,設(shè)置為NO专控,就不自動(dòng)消失
MGPopAction *action = [MGPopAction actionWithTitle:@"登錄" action:^{
}];
action.autoDismiss = NO;
2.利用MGPopController里dismiss方法,在需要的時(shí)候調(diào)用就可以關(guān)閉彈出框了
//模擬http請(qǐng)求異步返回?cái)?shù)據(jù)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"登錄成功...");
[self.loginController dismiss];
});
- 如何做到彈出框隱藏時(shí)遏餐,先隱藏鍵盤(pán)伦腐,再隱藏彈出框
知道這個(gè)問(wèn)題的我想肯定被UIAlertController折磨過(guò),因?yàn)樵赨IAlertController里失都,是沒(méi)法做到先隱藏鍵盤(pán)柏蘑,再隱藏提示框的,至少我沒(méi)有找到解決辦法(如果有人知道請(qǐng)你一定要告訴我_)
處理方法是在操作按鈕的action里去隱藏鍵盤(pán)粹庞,因?yàn)槲以贛GPopController里是先調(diào)用的action這個(gè)block咳焚,再隱藏的彈出框,demo里我是利用IQKeyboardManager處理的(其實(shí)把這個(gè)功能封裝到MGPopController里也是可以的):
[pop addAction:[MGPopAction actionWithTitle:@"關(guān)閉" action:^{
NSLog(@"關(guān)閉...");
[[IQKeyboardManager sharedManager] resignFirstResponder];
}]];
- 關(guān)于鍵盤(pán)的處理
輸入框會(huì)被彈起的鍵盤(pán)遮擋住庞溜,這個(gè)問(wèn)題很常見(jiàn)革半,相信大家肯定遇到過(guò)。然而我在MGPopController里并沒(méi)有增加對(duì)鍵盤(pán)的處理流码,因?yàn)閷?shí)際項(xiàng)目中我一般都會(huì)使用IQKeyboardManager這個(gè)第三方庫(kù)來(lái)處理鍵盤(pán)又官,你幾乎不用寫(xiě)一行代碼就可以完美解決這個(gè)問(wèn)題。我在demo里也引入了這個(gè)庫(kù)漫试,測(cè)試過(guò)后效果六敬,雖然不是很完美,但是也還可以驾荣。如果你接受不了外构,你可以自己添加對(duì)鍵盤(pán)的處理,思路大概如下:
1.在MGPopController里注冊(cè)鍵盤(pán)通知
2.根據(jù)需要播掷,動(dòng)態(tài)修改containerView(彈出框的所有View都添加到這個(gè)View里的)垂直方向偏移量审编,可以適當(dāng)添加點(diǎn)動(dòng)畫(huà),不至于顯得很生硬歧匈。
3.別忘了割笙,在dealloc里移除鍵盤(pán)通知
最后
UI不是重點(diǎn),重點(diǎn)是思路眯亦,希望對(duì)你能有一丟丟幫助伤溉。Enjoy yourself!
(文筆妻率、技術(shù)能力有限乱顾,如果發(fā)現(xiàn)哪里寫(xiě)錯(cuò)了或者說(shuō)錯(cuò)了,非常歡迎您能不吝賜教宫静,感激不盡W呔弧)