在UITextView的键盘中return键是用于换行的,所以不能像UITextField那样点击return来回收键盘。
#import <UIKit/UIKit.h>
@interface FeedBackViewController : UIViewController<UITextViewDelegate>
@property(nonatomic,retain)UITextView *feedBackTV;
@end
@implementation FeedBackViewController
@synthesize feedBackTV;
- (void)viewDidLoad
{
[super viewDidLoad];
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
//UITextView
self.feedBackTV = [[UITextView alloc] initWithFrame:CGRectMake(20, 65, 280, 300)];
self.feedBackTV.backgroundColor = [UIColor clearColor];
[self.feedBackTV.layer setBorderColor:[[UIColor grayColor] CGColor]];
[self.feedBackTV.layer setBorderWidth:1.0];
self.feedBackTV.delegate = self;
[self.view addSubview:self.feedBackTV];
//键盘上方按钮,用于键盘回落
UIToolbar *topView = [[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];
[topView setBarStyle:UIBarStyleBlack];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
NSArray *buttonsArray = [NSArray arrayWithObjects:doneButton,nil];
[doneButton release];
[topView setItems:buttonsArray];
[self.feedBackTV setInputAccessoryView:topView];
}
#pragma mark - UITextViewDelegate
//开始编辑UITextView时调用的代理方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
//开始编辑时使整个视图整体向上移
[UIView beginAnimations:@"up" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.view.frame = CGRectMake(0, -40, 320, 480);
[UIView commitAnimations];
return YES;
}
//键盘回落
- (void)dismissKeyBoard
{
[self.feedBackTV resignFirstResponder];
[UIView beginAnimations:@"up" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.view.frame = CGRectMake(0, 20, 320, 480);
[UIView commitAnimations];
}