iOS keyboard popup text input box Move up

Source: Internet
Author: User

Just started to do iOS, do not good, but also hope that friends a lot of advice, thank you!

#import "ViewController.h"@interfaceViewcontroller () <UITextFieldDelegate>{UIView*ActiveView; Uitextfield*TextField; floatKeyboardheight; floatW, H;}@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; W=Self.view.bounds.size.width; H=Self.view.bounds.size.height; Self.view.backgroundColor=[Uicolor Whitecolor]; ActiveView= [[UIView alloc] Initwithframe:cgrectmake (0, H- +W $)]; Activeview.backgroundcolor= [Uicolor colorwithred:0.4Green0.3Blue0.5Alpha1.0];            [Self.view Addsubview:activeview]; TextField= [[Uitextfield alloc] Initwithframe:cgrectmake (Ten,Ten, W -, -)]; Textfield.backgroundcolor=[Uicolor Lightgraycolor]; Textfield.keyboardappearance=Uitextborderstyleroundedrect; TextField.Delegate=Self ;        [ActiveView Addsubview:textfield]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwillappear:) Name: UikeyboardwillshownotificationObject: nil];}-(BOOL) textfieldshouldbeginediting: (Uitextfield *) textfield{[[Nsnotificationcenter Defaultcenter] Postnotificationname:uikeyboardwillshownotificationObject: nil]; returnYES;}- (void) Keyboardwillappear: (nsnotification*) noti{nsdictionary*keybinfo =[Noti UserInfo]; Nsvalue*value =[Keybinfo Objectforkey:uikeyboardframeenduserinfokey]; CGRect Keyboardframe=[value cgrectvalue]; Keyboardheight=KeyboardFrame.size.height; [UIView animatewithduration:1animations:^{cgrect Tframe=Activeview.frame; TFRAME.ORIGIN.Y= H-keyboardheight- +; Activeview.frame=Tframe; }];}- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{[TextField resignfirstresponder]; [UIView animatewithduration:0.3animations:^{cgrect Tframe=Activeview.frame; TFRAME.ORIGIN.Y= h- +; Activeview.frame=Tframe; }];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

iOS keyboard popup text input box Move up

Related Article

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.