网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

iOS屏幕根据键盘自动变化高度

一、效果图

成都创新互联是一家朝气蓬勃的网站建设公司。公司专注于为企业提供信息化建设解决方案。从事网站开发,网站制作,网站设计,网站模板,微信公众号开发,软件开发,小程序开发,10年建站对成都混凝土泵车等多个方面,拥有多年的网站推广经验。

iOS屏幕根据键盘自动变化高度

二、代码

ViewController.h

#import 

@interface ViewController : UIViewController


@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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网站栏目:iOS屏幕根据键盘自动变化高度
链接地址:http://bjjierui.cn/article/gihioo.html

其他资讯