iOS 键盘

在IOS开发中,如果输入框(UITextFiled)在界面的下半部分,那么,当键盘出现的时候,势必会挡住输入框,

这样的效果造成了很不好的用户体验,在这一点上,Android或许就做的好一点,在Android中,只要有输入框的地方,不管EditText处在界面什么位置,只要激发键盘开始输入,系统会自动把EditText至于键盘最上方(有的跟布局有关),这样不会遮挡住用户视线。在IOS中,关于这个问题,我参考了一定的解决方案,并自己总结了一下。

主要功能包括:

  • 自适应键盘出现后View的高度调整,防止遮挡输入框
1.首先在LoginViewController中实现UITextField的一个Delegate

//  Created by ken on 13-7-21.

//  Copyright (c) 2013 ken. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface LoginViewController : UIViewController<UITextFieldDelegate>


@property (retainnonatomicIBOutlet UITextField *userNumber;

@property (retainnonatomicIBOutlet UITextField *userPassword;


2.实现UITextFiledDelegate中的协议方法

- (void)viewDidLoad

{

    [super viewDidLoad];


    self.userNumber.delegate = self;

    self.userPassword.delegate = self;


}


//UITextField的协议方法,当开始编辑时监听

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    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;

    //上移30个单位,按实际情况设置

    CGRect rect=CGRectMake(0.0f,-30,width,height);

    self.view.frame=rect;

    [UIView commitAnimations];

    return YES;

}

//UITextField的协议方法,当结束编辑时监听

- (void) textFieldDidEndEditing:(UITextField *)textField{

    [self resumeView];

}


//恢复原始视图位置

-(void)resumeView

{

    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;

    //如果当前View是父视图,则Y20个像素高度,如果当前View为其他View的子视图,则动态调节Y的高度

    float Y = 20.0f;

    CGRect rect=CGRectMake(0.0f,Y,width,height);

    self.view.frame=rect;

    [UIView commitAnimations];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值