//初始化一个UITextField,在.h文件里实现<UITextFieldDelegate>协议
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController<UITextFieldDelegate>
@property(nonatomic,retain)UIProgressView *progress;
@end
UITextField *tex = [[UITextField alloc] initWithFrame:CGRectMake(10,260, 300, 40)];
tex.borderStyle = UITextBorderStyleRoundedRect;
tex.delegate = self;
[self.view addSubview:tex];
[tex release];
//是否可以输入
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
//键盘出现后当前view整体上移
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
// CGRectOffset,让一个矩阵整体偏移
// self.view.frame = CGRectOffset(self.view.frame, 0, -100);
CGRect target = CGRectOffset(self.view.frame, 0, -100);
[UIView animateWithDuration:0.3 animations:^{self.view.frame=target;}];
添加动画
}
//单击键盘return回复原始状态
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder]; //通知文本失去第一响应者状态
CGRect target = CGRectOffset(self.view.frame, 0, +100);
[UIView animateWithDuration:0.3 animations:^{
self.view.frame = target;
}];
return YES;
}