iOS屏幕根据键盘自动变化高度
程序员文章站
2023-12-19 14:14:46
一、效果图
二、代码
viewcontroller.h
#import
@interface viewc...
一、效果图
二、代码
viewcontroller.h
#import <uikit/uikit.h> @interface viewcontroller : uiviewcontroller <uitextfielddelegate> @end
viewcontroller.m
#import "viewcontroller.h" #define w [uiscreen mainscreen].bounds.size.width #define h [uiscreen mainscreen].bounds.size.height @interface viewcontroller () @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view, typically from a nib. uitextfield *field=[[uitextfield alloc]initwithframe:cgrectmake(100, 300, 50, 50)]; field.backgroundcolor=[uicolor redcolor]; field.delegate=self; [self.view addsubview:field]; } #pragma -mark -uitextfielddelegate -(bool)textfieldshouldreturn:(uitextfield *)textfield { [textfield resignfirstresponder]; //self.view 恢复原位 nstimeinterval animationduration=0.10f; [uiview beginanimations:@"resizeforkeyboard" context:nil]; [uiview setanimationduration:animationduration]; float width = w; float height = h; float y = 0.0f; cgrect rect=cgrectmake(0.0f,y,width,height); self.view.frame=rect; [uiview commitanimations]; return yes; } -(bool)textfieldshouldbeginediting:(uitextfield *)textfield { nstimeinterval animationduration=0.30f; [uiview beginanimations:@"resizeforkeyboard" context:nil]; [uiview setanimationduration:animationduration]; float width = w; float height = h; //上移100个单位,按实际情况设置 cgrect rect=cgrectmake(0.0f,-100,width,height); self.view.frame=rect; [uiview commitanimations]; return yes; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。