IOS dynamically changes the height of the UIView based on the height of the keyboard

Source: Internet
Author: User

When we use the keyboard often appear in the keyboard blocking the view of this situation, I would like to introduce a method can be based on the height of the keyboard to dynamically change the degree of view so that it can always be on the top of the keyboard

In this view I use TextView

uikeyboardwillshownotification//keyboard pop-up uikeyboardwillhidenotification//keyboard retract//popup with notification listening keyboard
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboarshow:) Name: Uikeyboardwillshownotification Object:nil];

Retract the listening keyboard
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardhiden:) Name: Uikeyboardwillhidenotification Object:nil];

Set delegate
_mytextview.delegate=self;


Additional setup after loading the view, typically from a nib.
}
Show
-(void) Keyboarshow: (nsnotification *) notification{
Get the height of the keyboard
CGRect rect =[notification.userinfo[uikeyboardframeenduserinfokey] cgrectvalue];
Calculate the height of the TextView
CGRect Textviewrect=_mytextview.frame;
CGFloat height=textviewrect.size.height-rect.size.height;
Textviewrect.size.height=height;
[UIView animatewithduration:.2 animations:^{
Self.mytextview.frame=textviewrect;
}];} Called when the keyboard is hidden-(void) Keyboardhiden: (nsnotification *) notification{
_mytextview.frame=self.view.frame;
This will do what we want to do if you have any questions to ask. We study together

IOS dynamically changes the height of the UIView based on the height of the keyboard

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.