Cat Share, must boutique
Material code Address: http://download.csdn.net/detail/u013357243/8614731
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents
First look at the effect
Code NYVIEWCONTROLLER.M
////NYVIEWCONTROLLER.M//Registration////Created by Apple on 15-4-19.//Copyright (c) 2015 Znycat. All rights reserved.//#import "NYViewController.h" #import "NYKeyboardTool.h" @interface nyviewcontroller () <nykeyboardtooldelegate>{ Nsarray*_fields;//Storage of all TextField}//Input box container view@property(Weak,nonatomic)Iboutlet UIView*inputcontainer;@property(Weak,nonatomic)IboutletUitextfield *birthdayfield;@end @implementation nyviewcontroller - (void) viewdidload{[SuperViewdidload];//1: Initialize the custom keyboard[ SelfSetupcustomkeyboard];//2: Set the keyboard toolbar for each textfield (Inputaccessoryview)[ SelfSetupkeyboardtool];//3: Listen for keyboard events[ SelfSetupkeyboardnotification];}//1: Initialize the custom keyboard-(void) setupcustomkeyboard{Uidatepicker *datepicker = [[Uidatepicker alloc] init];//Settings AreaDatePicker. Locale= [Nslocale localewithlocaleidentifier:@"en"];//Setup modeDatePicker. Datepickermode= Uidatepickermodedate;//Put the DatePicker in the birthday keyboard Self. Birthdayfield. Inputview= DatePicker;}//2: Set the keyboard toolbar for each textfield (Inputaccessoryview)-(void) setupkeyboardtool{//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; } }}//3: Listen for keyboard events-(void) setupkeyboardnotification{//Set up monitoring[[NsnotificationcenterDefaultcenter]addobserver: SelfSelector@selector(Kbframechange:) Name:uikeyboardwillchangeframenotification object:Nil];}//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; }]; }}//Gets the index of the responder of the current TextField in the _fields//Return-1 means not found-(int) getcurrentresponderindex{//Traverse all TextField to get response values for(Uitextfield *TF in _fields) {if(TF. Isfirstresponder) {return[_fields INDEXOFOBJECT:TF]; } }return-1;}//How to start touch-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{//Turn off the keyboard[ Self. ViewEndediting:YES];}#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]; }}@end
Customizing the toolbar Code
NYKeyboardTool.h
////NYKeyboardTool.h//Registration////Created by Apple on 15-4-19.//Copyright (c) 2015 Znycat. All rights reserved.//#import <UIKit/UIKit.h> @class nykeyboardtool;typedef enum{keyboarditemtypeprevious,//PreviousKeyboarditemtypenext,//NextKeyboarditemtypedone//Completed}keyboarditemtype; @protocol nykeyboardtooldelegate <nsobject>-(void) Keyboardtool: (Nykeyboardtool *) Keyboardtool Didclickitemtype: (keyboarditemtype) ItemType;@end @interface nykeyboardtool : uitableviewheaderfooterview //Add Agent@property(nonatomic,Weak)ID<NYKeyboardToolDelegate> delegate;//Create Objects+ (instancetype) Keyboardtool;@end
nykeyboardtool.m
////NYKEYBOARDTOOL.M//Registration////Created by Apple on 15-4-19.//Copyright (c) 2015 Znycat. All rights reserved.//#Import "NYKeyboardTool.h"@interfaceNykeyboardtool ()/** prev * /-(Ibaction) Previous: (ID) sender;/** Next * /-(Ibaction) Next: (ID) sender;/** completed * /-(Ibaction) Done: (ID) sender;@end@implementationNykeyboardtool//Create Objects+ (Instancetype) keyboardtool{return[[[NSBundle mainbundle]loadnibnamed:@"Nykeyboardtool"Owner:nil Options:nil]lastobject];} -(Ibaction) Previous: (ID) Sender {//Determine if the agent has implemented the appropriate method. if([Self.delegate Respondstoselector:@selector(Keyboardtool:didclickitemtype:)]) {[Self.delegate keyboardtool:self didclickitemtype:keyboarditemtypeprevious]; }}-(Ibaction) Next: (ID) Sender {//Determine if the agent has implemented the appropriate method. if([Self.delegate Respondstoselector:@selector(Keyboardtool:didclickitemtype:)]) {[Self.delegate keyboardtool:self didclickitemtype:keyboarditemtypenext]; }}-(ibaction) Done: (ID) Sender {//Determine if the agent has implemented the appropriate method. if([Self.delegate Respondstoselector:@selector(Keyboardtool:didclickitemtype:)]) {[Self.delegate keyboardtool:self didclickitemtype:keyboarditemtypedone]; }}@end
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
(material source) Cat learn iOS (24) UI registration case