#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController<UITextFieldDelegate> {
UITextField *textField1;
UITextField *textField2;
}
@property (nonatomic,retain) UITextField *textField1;
@property (nonatomic ,retain) UITextField *textField2;
-(IBAction)backgroundTap:(id)sender;
@end
#import "RootViewController.h"
@implementation RootViewController
@synthesize textField1;
@synthesize textField2;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
UIView *back = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
back.backgroundColor = [UIColor grayColor];
self.view = back;
[back release];
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
UIControl *_back = [[UIControl alloc] initWithFrame:self.view.frame];
_back.backgroundColor = [UIColor grayColor];
self.view = _back;
[_back release];
[(UIControl *)self.view addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchDown];
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 200, 30)];
textField1.backgroundColor = [UIColor clearColor];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.textColor = [UIColor redColor];
textField1.delegate = self;
[self.view addSubview:textField1];
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(20, 30, 200, 30)];
textField2.backgroundColor = [UIColor clearColor];
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.textColor = [UIColor redColor];
textField2.delegate = self;
[self.view addSubview:textField2];
}
#pragma mark -
#pragma mark 解决虚拟键盘挡住UITextField的方法
- (void)keyboardWillShow:(NSNotification *)noti
{
//键盘输入的界面调整
//键盘的高度
float height = 216.0;
CGRect frame = self.view.frame;
frame.size = CGSizeMake(frame.size.width, frame.size.height - height);
[UIView beginAnimations:@"Curl"context:nil];//动画开始
[UIView setAnimationDuration:0.30];
[UIView setAnimationDelegate:self];
[self.view setFrame:frame];
[UIView commitAnimations];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame = rect;
[UIView commitAnimations];
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect frame = textField.frame;
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
if(offset > 0)
{
CGRect rect = CGRectMake(0.0f, -offset,width,height);
self.view.frame = rect;
}
[UIView commitAnimations];
}
#pragma mark -
#pragma mark -
#pragma mark 触摸背景来关闭虚拟键盘
-(IBAction)backgroundTap:(id)sender
{
// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame = rect;
[UIView commitAnimations];
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
}
#pragma mark -
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[textField1 release];
[textField2 release];
[super dealloc];
}
这个方法用的时候首先把RootViewController上的view改成UIControl,然后通过UIControl的事件UIControlEventTouchDown来触发上面的方法backgroundTap: 。
UIControl *_back = [[UIControl alloc] initWithFrame:self.view.frame];
_back.backgroundColor = [UIColor grayColor];
self.view = _back;
[_back release];
[(UIControl *)self.view addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchDown];
相关推荐
下载解压文件,替换掉Qt安装目录的gcc_64下对应文件,比如Linux下安装目录/opt/Qt5.9.2/5.9.2/gcc_64目录,Qt程序中调出来的默认就是可以输入中文的虚拟键盘
实现键盘的点击隐藏和解决textField被呼出键盘挡住的问题的简单代码
iOS textField弹出自定义键盘(弹出日期时间、省市、国家等类型的键盘)
http://blog.csdn.net/duxinfeng2010/article/category/1155790 有详细解释
自动控制viewController的textView和textField被键盘遮挡问题,引入.h即可
TextField和键盘上移位置,tableView自动上移到键盘上边的位置
IOS 键盘挡住输入框的问题解决办法 在iOS开发发现一个问题,有时输入框位于低出时,当编辑输入时,弹出的键盘会挡住输入框,令用户看不清楚实时的输入情况,使界面交互极度不友好。 经过查资料终于解决了这个问题。 ...
网友做的一个demo 键盘挡住textField的问题
自定义textfield
手工创建TextField,TextField的属性基本上上面都描述了,以及键盘的回调方法
LXM 键盘管理器一个通过监听键盘然后调整textField位置使之不会被键盘挡住的工具类效果图如下: 来自 ##安装需要iOS5.0或更高版本使用 cocoaPods 1、将 LXMKeyboardManager pod 添加到你的 Podfile pod '...
自定义输入textfield,这个源码是在源码天堂那个转载过来的,具体大家可以到那里看看一下吧,希望大家能够喜欢。 演示图:http://code.662p.com/view/10936.html
使用一个子TextField,当TextField被填充时,提示将浮现在上方
oc TextField 键盘弹出移动位置 //上移后,textField需要额外高于键盘顶部的距离,默认为0 @property (nonatomic, assign) CGFloat offset; //需要向上移动的view,默认为keyWindow @property (nonatomic, weak) ...
本文通过实例代码给大家介绍了OS中只让textField使用键盘通知的操作方法,代码简单易懂,非常不错,具有参考借鉴加载,需要的的朋友参考下吧
Java组件的使用(Bottun,TextField等的使用方法,适合初学Java的人员参考)
一行代码解决textField输入中文长度限制 一行代码解决textField输入中文长度限制 一行代码解决textField输入中文长度限制
ExtJs5 去掉textfield边框
8-22textfield8-22textfield8-22textfield8-22textfield
TextField 自动换行