两种方式隐藏键盘

来源:互联网 发布:鼎尖软件 编辑:程序博客网 时间:2024/04/29 17:09

- (void)viewDidLoad {
    [super viewDidLoad];
   UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard:)];
    tapGestureRecognizer.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tapGestureRecognizer];
   
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0,320,30)];
    [topView setBarStyle:UIBarStyleBlackTranslucent];
    UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle:@"Hello" style:UIBarButtonItemStyleBordered target:self action:nil];        
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(hideKeyBoard:)];
    NSArray * buttonsArray = [NSArray arrayWithObjects:helloButton,btnSpace,doneButton,nil];
    [doneButton release];
    [btnSpace release];
    [helloButton release];
    [topView setItems:buttonsArray];
    [textField setInputAccessoryView:topView];  

}

- (void)hideKeyBoard:(id)sender
{
    [textField  resignFirstResponder];
}

原创粉丝点击