The keyboard appears in the Hide (solve the problem that will overwrite the text box when the keyboard pops up, code implementation)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.