UITextView键盘回落

来源:互联网 发布:球球大作战开挂软件 编辑:程序博客网 时间:2024/05/01 14:21

在UITextView的键盘中return键是用于换行的,所以不能像UITextField那样点击return来回收键盘。


#import<UIKit/UIKit.h>

@interface FeedBackViewController :UIViewController<UITextViewDelegate>

@property(nonatomic,retain)UITextView *feedBackTV;

@end



@implementation FeedBackViewController

@synthesize feedBackTV;


- (void)viewDidLoad

{

    [superviewDidLoad];    

   self.view = [[[UIViewalloc] initWithFrame:CGRectMake(0,0, 320,460)] autorelease];

//UITextView

   self.feedBackTV = [[UITextViewalloc] initWithFrame:CGRectMake(20,65, 280, 300)];

   self.feedBackTV.backgroundColor = [UIColorclearColor];

    [self.feedBackTV.layersetBorderColor:[[UIColorgrayColor] CGColor]];

    [self.feedBackTV.layersetBorderWidth:1.0];

    self.feedBackTV.delegate =self;

    [self.viewaddSubview:self.feedBackTV];


//键盘上方按钮,用于键盘回落

   UIToolbar *topView = [[[UIToolbaralloc]initWithFrame:CGRectMake(0,0, 320,40)] autorelease];  

    [topViewsetBarStyle:UIBarStyleBlack];  

   UIBarButtonItem *doneButton = [[UIBarButtonItemalloc]initWithTitle:@"Done"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(dismissKeyBoard)];  

    NSArray *buttonsArray = [NSArray arrayWithObjects:doneButton,nil];  

    [doneButton release];  

    [topView setItems:buttonsArray];  

    [self.feedBackTVsetInputAccessoryView:topView]; 


}



#pragma mark - UITextViewDelegate


//开始编辑UITextView时调用的代理方法

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView

{

    //开始编辑时使整个视图整体向上移

    [UIViewbeginAnimations:@"up"context:nil];

    [UIViewsetAnimationDuration:0.5];

    [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

    self.view.frame =CGRectMake(0, -40,320, 480);

    [UIViewcommitAnimations];

   return YES;

}


//键盘回落

- (void)dismissKeyBoard  

{  

    [self.feedBackTVresignFirstResponder];

    [UIViewbeginAnimations:@"up"context:nil];

    [UIViewsetAnimationDuration:0.5];

    [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

    self.view.frame =CGRectMake(0,20, 320, 480);

    [UIViewcommitAnimations];





原创粉丝点击