Cat Share, must boutique
Material code Address: http://blog.csdn.net/u013357243/article/details/45165591
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents
First look at the effect
Making ideas
Cat in doing this, first use Stroyboard to draw out the interface UI, this is very simple, not much to say, and then the next step is to customize the xib to do the keyboard above the column, you need to do xib and view, detailed code below will have, when we click, Our keyboard frame changes to form a pop-up effect, this in front of the QQ case has, in fact, is the use of notifications, where we customize the toolbar "last" "Next" "Finish" these buttons, we use the way to do the agent.
Code, Attention Point Agent
First of all we have to write our own agent:
@protocol NYKeyboardToolDelegate <NSObject> -(void)keyboardTool:(NYKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType;@end
And then we're going to set up the proxy class and let him implement our agreement.
@interface NYViewController () <NYKeyboardToolDelegate>
Create the toolbar, set up the proxy, traverse all the Uitextfield into the global variable _fields, set the properties
//Create toolbarsNykeyboardtool *keyboardtool = [Nykeyboardtool Keyboardtool];//Set up proxyKeyboardtool. Delegate= Self;//1, gets all the child controls of the input box container Nsarray*views = [ Self. InputcontainerSubviews];//Create an array to store TextField Nsmutablearray*FIELDM = [NsmutablearrayArray];//2, traversing for(UIView*childview in views) {//If the child control is Uitextfield, set Inputaccessoryview if([Childview Iskindofclass:[uitextfield class]]) {Uitextfield *TF = (Uitextfield *) Childview; Tf. Inputaccessoryview= Keyboardtool;//Add the TextField to the array. [FIELDM ADDOBJECT:TF]; _fields = FIELDM; } }
Remember to implement the Protocol method otherwise the toolbar button is useless
#pragma agent for Mark-nykeyboardtooldelegate Agent Keyboard toolbar-(void) Keyboardtool: (Nykeyboardtool *) Keyboardtool Didclickitemtype: (keyboarditemtype) itemtype{//Gets the index of the current responder intCurrentindex = [ SelfGetcurrentresponderindex];NSLog(@"Current responder%d", Currentindex);if(ItemType = = keyboarditemtypeprevious) {NSLog(@"Previous");//Make previous field successful responder[ SelfShowproviousfield:currentindex]; }Else if(ItemType = = Keyboarditemtypenext) {NSLog(@"Next");//Let the next field successful responder[ SelfShownextfield:currentindex]; }Else{NSLog(@"Done"); [ SelfTouchesbegan:NilWithevent:Nil]; }}//Make previous field successful responder-(void) Showproviousfield: (int) currentindex{intProviousindex = Currentindex-1;if(Proviousindex >=0) {Uitextfield *PROVIOUSTF = [_fields objectatindex:proviousindex]; [Provioustf Becomefirstresponder]; }}//Let the next field successful responder-(void) Shownextfield: (int) currentindex{intNextindex = Currentindex +1;//The next index cannot exceed the number of _fields arrays if(Nextindex < _fields. Count) {//Allow current responders to distributeUitextfield *currenttf = [_fields objectatindex:currentindex]; [Currenttf Resignfirstresponder]; Uitextfield *nexttf = [_fields objectatindex:nextindex]; [Nexttf Becomefirstresponder]; }}
The frame changes animation of the keyboard, there are also notifications
Initializing a custom keyboard
//1:初始化自定义键盘-(void)setupCustomKeyboard{ UIDatePicker *datePicker = [[UIDatePicker alloc] init]; //设置地区 datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"]; //设置模式 datePicker.datePickerMode = UIDatePickerModeDate; //把datePicker放到birthday的键盘中 self.birthdayField.inputView = datePicker;}
Keyboard frame changes in addition to notification of activation
Set up monitoring (notification)
//建立监听 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
Changes in keyboard frame
//Shift of the frame of the keyboard-(void) Kbframechange: (nsnotification*) noti{//Change the background color of window Self. View. Window. BackgroundColor= Self. Inputaccessoryview. BackgroundColor;//Keyboard exit frame CGRectframe = [Noti. UserInfo[Uikeyboardframeenduserinfokey] Cgrectvalue];//Keyboard real-time y CGFloatKbendy = Frame. Origin. Y;//Get the current responder intCurrentindex = [ SelfGetcurrentresponderindex]; Uitextfield *currenttf = _fields[currentindex];//height to be changed: CGFloatTfmaxy = Cgrectgetmaxy (Currenttf. Frame) + Self. Inputcontainer. Frame. Origin. Y;//If the maximum value of TextField is the Y seat of the keyboard, move up if(Tfmaxy > Kbendy) { [UIViewAnimatewithduration:0.25animations:^{ Self. View. Transform= Cgaffinetransformmaketranslation (0, Kbendy-tfmaxy); }]; }Else{ [UIViewAnimatewithduration:0.25animations:^{ Self. View. Transform= Cgaffinetransformidentity; }]; } }
Other small places
Turn off the keyboard
//开始触摸的方法-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //关闭键盘 [self.view endEditing:YES];}
There's a lot of uitextfield in the view. Then we want to know which one has the keyboard open, which is to get the responder
//获取当前 textField的响应者在_fields中的索引//返回-1代表没有找到-(int)getCurrentResponderIndex{ //遍历所有的textField获取响应值 forin _fields) { if (tf.isFirstResponder) { return [_fields indexOfObject:tf]; } } return -1;}
Uidatepicker Use of date keyboards:
UIDatePicker *datePicker = [[UIDatePicker alloc] init]; //设置地区 datePicker.locale = [NSLocalelocaleWithLocaleIdentifier:@"zh"]; //设置模式 UIDatePickerModeDate; //把datePicker放到birthday的键盘中 self.birthdayField.inputView = datePicker;
PS: New iOS Exchange Learning Group: 304570962 can add cat qq:1764541256 or Znycat Let's study hard together.
Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents
Cat Learn iOS (24) UI registration case