iOS屏幕根據(jù)鍵盤自動變化高度
更新時間:2017年07月06日 11:41:57 作者:弦外雨
這篇文章主要為大家詳細介紹了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í)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- iOS開發(fā)中使用屏幕旋轉(zhuǎn)功能的相關(guān)方法
- iOS開發(fā)中控制屏幕旋轉(zhuǎn)的編寫方法小結(jié)
- 總結(jié)iOS App開發(fā)中控制屏幕旋轉(zhuǎn)的幾種方式
- iOS開發(fā)之使用Storyboard預(yù)覽UI在不同屏幕上的運行效果
- iOS應(yīng)用開發(fā)中使用Auto Layout來適配不同屏幕尺寸
- IOS開發(fā)中鍵盤輸入屏幕上移的解決方法
- iOS如何獲取屏幕寬高、設(shè)備型號、系統(tǒng)版本信息
- iOS屏幕適配開發(fā)實用技巧
- iOS中滑動控制屏幕亮度和系統(tǒng)音量(附加AVAudioPlayer基本用法和Masonry簡單使用)
- iOS 中根據(jù)屏幕寬度自適應(yīng)分布按鈕的實例代碼
相關(guān)文章
iOS 基本動畫、關(guān)鍵幀動畫、利用緩動函數(shù)實現(xiàn)物理動畫效果
這篇文章主要介紹了iOS 基本動畫、關(guān)鍵幀動畫、利用緩動函數(shù)實現(xiàn)物理動畫效果的相關(guān)資料,需要的朋友可以參考下2016-10-10IOS 開發(fā)之UITableView 刪除表格單元寫法
這篇文章主要介紹了IOS 開發(fā)之UITableView 刪除表格單元寫法的相關(guān)資料,這里提供實例幫助大家實現(xiàn)該功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08Objective-C編程中語句和變量的一些編寫規(guī)范建議
這篇文章主要介紹了Objective-C編程中語句和變量的一些編寫規(guī)范建議,包括三目運算符和錯誤處理等方面,以及對變量命名的書寫建議,需要的朋友可以參考下2016-04-04iOS遍歷集合(NSArray、NSDictionary、NSSet)的方法總結(jié)
這篇文章主要介紹了iOS集合遍歷(NSArray、NSDictionary、NSSet)的方法,文中給出了詳細的方法示例,并總結(jié)了各個方法的優(yōu)缺點來供大家學(xué)習(xí)參考,需要的朋友們下面來一起看看吧。2017-03-03