记录于/6/26:
一.点击键盘上的“Done”按钮关闭键盘
1.视图控制器实现UITextFieldDelegate协议
2.设置文本框属性
textField.returnKeyType = UIReturnKeyDone;
textField.delegate=self;
3. 重写协议方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textFieldresignFirstResponder];
// the user pressed the "Done" button, so dismiss the keyboard
returnYES;
}
二.点击键盘上的“Next”按钮实现文本框焦点跳转
1.视图控制器实现UITextFieldDelegate协议
2.设置文本框属性
tx_username.returnKeyType = UIReturnKeyNext;
tx_username.delegate =self;
3.重写协议方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if([tx_username isFirstResponder]) {
[tx_email becomeFirstResponder];
}elseif([tx_email isFirstResponder]) {
[tx_password becomeFirstResponder];
} elseif([tx_password isFirstResponder]) {
[tx_conPassword becomeFirstResponder];
} elseif([tx_conPassword isFirstResponder]) {
[self performSelector:@selector(userRegister)];//tx_conPassword指定了UIReturnKeyJoin类型后调用的方法
}
returnYES;
}
UIScrollView页面下点击TextField键盘事件:
不能改变scrollView的frame,得改变contentOffSet调整。
(1)
#pragma mark
#pragma mark - Textfield delegate
//增加手势 点击输入框以外区域降下键盘
-(void)RegisterViewAddGest:(UIImageView *)view {
view.userInteractionEnabled = YES;
//单击事件
UITapGestureRecognizer *taprecognizer;
taprecognizer = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(RegisterKeyDown:)];
taprecognizer.numberOfTapsRequired = 1;
[view addGestureRecognizer:taprecognizer];
[taprecognizer release];
}
//键盘下降的时候
-(void)RegisterKeyDown:(UITapGestureRecognizer *)taprecognizer{
DebugLog(@"点击背景键盘下降");
if (taprecognizer.numberOfTapsRequired == 1) {
[self.nameTextFieldresignFirstResponder];
[self.phoneTextFieldresignFirstResponder];
[self.addressTextFieldresignFirstResponder];
[self.goodsTextFieldresignFirstResponder];
[self.numberTextFieldresignFirstResponder];
[self.timeTextFieldresignFirstResponder];
[self.attendTextFieldresignFirstResponder];
NSTimeInterval animationDuration = 0.30f;
[UIViewbeginAnimations:@"ResizeForKeyboard"context:nil];
[UIViewsetAnimationDuration:animationDuration];
if (self.buyNowScrollView.contentOffset.y >= self.buyNowScrollView.contentSize.height - self.buyNowScrollView.frame.size.height) {
self.buyNowScrollView.contentOffset = CGPointMake(0, self.buyNowScrollView.contentSize.height - self.buyNowScrollView.frame.size.height);
}
[UIViewcommitAnimations];
}
}
//输入框return的时候
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isEqual:self.nameTextField]) {
[self.phoneTextFieldbecomeFirstResponder];
}elseif ([textField isEqual:self.phoneTextField]) {
[self.addressTextFieldbecomeFirstResponder];
}elseif ([textField isEqual:self.addressTextField]) {
[self.goodsTextFieldbecomeFirstResponder];
}elseif ([textField isEqual:self.goodsTextField]) {
[self.numberTextFieldbecomeFirstResponder];
}elseif ([textField isEqual:self.numberTextField]) {
[self.timeTextFieldbecomeFirstResponder];
}elseif ([textField isEqual:self.timeTextField]) {
[self.attendTextFieldbecomeFirstResponder];
}else {
DebugLog(@"输入框return的时候");
[textField resignFirstResponder];
NSTimeInterval animationDuration = 0.30f;
[UIViewbeginAnimations:@"ResizeForKeyboard"context:nil];
[UIViewsetAnimationDuration:animationDuration];
self.buyNowScrollView.contentOffset = CGPointMake(0, self.buyNowScrollView.contentSize.height - self.buyNowScrollView.frame.size.height);
[UIViewcommitAnimations];
}
returnYES;
}
//输入框弹出的时候
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
DebugLog(@"键盘弹出");
CGRect frame = textField.frame;
int offset = frame.origin.y + 70 - (self.view.frame.size.height - 216.0);//键盘高度216
NSTimeInterval animationDuration = 0.30f;
[UIViewbeginAnimations:@"ResizeForKeyBoard"context:nil];
[UIViewsetAnimationDuration:animationDuration];
if(offset > 0)
{
self.buyNowScrollView.contentOffset = CGPointMake(0, offset + 10);
}else{
self.buyNowScrollView.contentOffset = CGPointMake(0, 0);
}
[UIViewcommitAnimations];
}
(2)改写(1)的,更加稳定适用了 暂时还是很完美的
里面的都是textField嵌入在scroll里面的使用
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//401为普通会员账号 402为普通会员密码 403为普通会员密码确认 404为普通会员昵称
//411为发型师会员账号 412为发型师会员密码 413为发型师会员密码确认 414为发型师会员姓名 415为发型师会员手机
//键盘高度216
if(textField.tag <= 404){
if ([textField isEqual:monUserText]) {
[monPassTextbecomeFirstResponder];
}elseif ([textField isEqual:monPassText]){
[monRepeatPassTextbecomeFirstResponder];
}elseif ([textField isEqual:monRepeatPassText]){
[monNickTextbecomeFirstResponder];
}else{
for (UIView *subView monScrollView.subviews) {
if ([subView isKindOfClass:[UITextFieldclass]]) {
[subView resignFirstResponder];
}
}
if (monScrollView.contentSize.height - monScrollView.contentOffset.y < monScrollView.frame.size.height) {
[monScrollViewsetContentOffset:CGPointMake(0, monScrollView.contentSize.height - monScrollView.frame.size.height) animated:YES];
}
}
}else{
if ([textField isEqual:self.barberUserText]) {
[self.barberPassTextbecomeFirstResponder];
}elseif ([textField isEqual:self.barberPassText]){
[self.barberRepeatPassTextbecomeFirstResponder];
}elseif ([textField isEqual:self.barberRepeatPassText]){
[self.barberNickTextbecomeFirstResponder];
}elseif ([textField isEqual:self.barberNickText]){
[self.barberTelphoneTextbecomeFirstResponder];
}else{
for (UIView *subView inself.barberScrollView.subviews) {
if ([subView isKindOfClass:[UITextFieldclass]]) {
[subView resignFirstResponder];
}
}
//scroll的内容高度 - scroll的contentOffSet高度 < scroll的frane高度
if (self.barberScrollView.contentSize.height - self.barberScrollView.contentOffset.y < self.barberScrollView.frame.size.height) {
[self.barberScrollViewsetContentOffset:CGPointMake(0, self.barberScrollView.contentSize.height - self.barberScrollView.frame.size.height) animated:YES];
}
}
}
returnYES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//401为普通会员账号 402为普通会员密码 403为普通会员密码确认 404为普通会员昵称
//411为发型师会员账号 412为发型师会员密码 413为发型师会员密码确认 414为发型师会员姓名 415为发型师会员手机
//键盘高度216
CGRect frame = textField.frame;
int offset;
if(textField.tag <= 404){
//scroll的y坐标 + textField在Scroll上的y坐标 + textField高度 - 除键盘高度外屏幕高度
offset = frame.origin.y + 110 + 30 + 6 - (SCREEN_HEIGHT - 252);
NSTimeInterval animationDuration = 0.30f;
[UIViewbeginAnimations:@"ResizeForKeyBoard"context:nil];
[UIViewsetAnimationDuration:animationDuration];
if(offset > 0)
{
[monScrollViewsetContentOffset:CGPointMake(0, offset) animated:YES];
}else{
[monScrollViewsetContentOffset:CGPointMake(0, 0) animated:YES];
}
[UIViewcommitAnimations];
}else {
NSLog(@"%f",SCREEN_HEIGHT);
offset = frame.origin.y + 146 - (SCREEN_HEIGHT - 252);
if(offset > 0)
{
[self.barberScrollViewsetContentOffset:CGPointMake(0, frame.origin.y + 146 - (SCREEN_HEIGHT - 252)) animated:YES];
}else{
[self.barberScrollViewsetContentOffset:CGPointMake(0, 0) animated:YES];
}
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
//scroll的内容高度 - scroll的contentOffSet高度 < scroll的frane高度
if (monScrollView.contentSize.height - monScrollView.contentOffset.y < monScrollView.frame.size.height) {
[monScrollViewsetContentOffset:CGPointMake(0, monScrollView.contentSize.height - monScrollView.frame.size.height) animated:YES];
}
if (self.barberScrollView.contentSize.height - self.barberScrollView.contentOffset.y < self.barberScrollView.frame.size.height) {
[self.barberScrollViewsetContentOffset:CGPointMake(0, self.barberScrollView.contentSize.height - self.barberScrollView.frame.size.height) animated:YES];
}
}
UIView下的界面移动避免键盘遮盖方法:
- (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, 0.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 + 70 - (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];
}