The keyboard appears in the Hide (solve the problem that will overwrite the text box when the keyboard pops up, code implementation)
#import "ViewController.h"
#import "Uiview+frameextension.h"//can write on their own, later use convenient
#define Kdeviceheight [UIScreen mainscreen].bounds.size.height
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Set the background color of a view
Self.view.backgroundColor = [Uicolor Lightgraycolor];
Add first text box assumed position
Uitextfield *firstfield = [[Uitextfield alloc]initwithframe:cgrectmake (50, 300, 200, 40)];
Firstfield.backgroundcolor = [Uicolor Whitecolor];
[Self.view Addsubview:firstfield];
Add a first text box
Uitextfield *secondfield = [[Uitextfield alloc]initwithframe:cgrectmake (firstfield.x, FirstField.bottom + 50, Firstfield.width, Firstfield.height)];
[Self.view Addsubview:secondfield];
Secondfield.backgroundcolor = [Uicolor Whitecolor];
Register the notification of the keyboard display
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (showkeyboard:) Name: Uikeyboardwillshownotification Object:nil];
Registering for keyboard-hidden notifications
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (hidekeyboard:) Name: Uikeyboardwillhidenotification Object:nil];
}
This method is performed when the keyboard pops up,
-(void) Showkeyboard: (nsnotification *) notification{
Define a text box that points to the text box you are editing, which is the text box that pops up the keyboard
Uitextfield *txtfield;
This time iterates through all the child views of the current view, the Subviews array holds all the child views of the current view
For (UIView *subview in self.view.subviews) {
If this sub-view is a text box, the Iskindofclass method can tell if a variable is a variable of a certain type.
if ([SubView Iskindofclass:[uitextfield class]]) {
Turn this child view into a text box first
Uitextfield *tempfield = (Uitextfield *) SubView;
and decide if the text box is being edited.
if (tempfield.isediting) {
If this text box is being edited, it's the text box I'm looking for, break loop
Txtfield = Tempfield;
Break
}
}
}
NSLog (@ "%@", notification);
Gets the UserInfo property of the notification
Nsdictionary *userinfodict = Notification.userinfo;
Gets the bounds of the keyboard via the UserInfo property of the keyboard notification
Nsvalue *value = [Userinfodict Objectforkey:uikeyboardboundsuserinfokey];
The size of the keyboard
Cgsize keyboardsize = [value cgrectvalue].size;
Keyboard height
CGFloat keyboardheight = keyboardsize.height;
CGFloat offset = Kdeviceheight-keyboardheight-txtfield.bottom;
if (Offset < 0) {//In this case need to move up
offset = offset-10; Save the height of the move up
[UIView animatewithduration:0.5 animations:^{
Self.view.transform = cgaffinetransformmaketranslation (0, offset);
}];
}
}
-(void) Hidekeyboard: (nsnotification *) notification{
[UIView Animatewithduration:2 animations:^{
Self.view.transform = cgaffinetransformidentity;
}];
}
Hide the keyboard when you tap a blank screen
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{
[Self.view Endediting:yes];
}
@end
Source files can be downloaded here, hoping to help you: Http://pan.baidu.com/s/1pLms9zP
The keyboard appears in the Hide (solve the problem that will overwrite the text box when the keyboard pops up, code implementation)