iOS屏幕根據(jù)鍵盤自動變化高度

一、效果圖

成都創(chuàng)新互聯(lián)是一家朝氣蓬勃的網(wǎng)站建設(shè)公司。公司專注于為企業(yè)提供信息化建設(shè)解決方案。從事網(wǎng)站開發(fā),網(wǎng)站制作,網(wǎng)站設(shè)計,網(wǎng)站模板,微信公眾號開發(fā),軟件開發(fā),小程序開發(fā),10年建站對成都混凝土泵車等多個方面,擁有多年的網(wǎng)站推廣經(jīng)驗。

iOS屏幕根據(jù)鍵盤自動變化高度

二、代碼

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 恢復(fù)原位
  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個單位,按實際情況設(shè)置
  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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前名稱:iOS屏幕根據(jù)鍵盤自動變化高度
文章分享:http://bm7419.com/article0/gihioo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、網(wǎng)站維護(hù)全網(wǎng)營銷推廣、軟件開發(fā)、企業(yè)網(wǎng)站制作、微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)