iosのUIsearchBar仮想キーボードを隠すいくつかの方法


iOSにソフトキーボードが表示されたら、キーボードが消えてもキーボードを非キーボード部分をクリックして隠す方法を検討します.
1つ目の方法:buttonを追加し、対応するtouch downイベントを追加し、キーボードを非表示にします.この方法は,あまりにもパクリだ.対応するイベントのためにbuttonを追加するのは価値がありません.
2つ目の方法:背景画像にTapイベントを追加し、対応するクリック処理を行います.この方法はbutton方式に取って代わったが,UIに背景画像がなければ,この方法はまた最初のパクリ方法の列に戻った.
[plain] view plain copy print ?
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  • - (void)viewDidLoad  

  • {  
  •     [super viewDidLoad];  

  •       
  • //処理時間付き背景画像
  • を追加
        UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];  
  •     backView.image = [UIImage imageNamed:@"small3.png"];  

  •       
  •     backView.userInteractionEnabled = YES;  

  •     UITapGestureRecognizer *singleTouch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];  
  •     [backView addGestureRecognizer:singleTouch];  

  •       
  •     backView.tag = 110;  

  •     [self.view addSubview:backView];  
  •       

  • //uitextfield の追加
  •     text = [[UITextField alloc] initWithFrame:CGRectMake(30, 150, 250, 31)];  

  •     //[text setBackgroundColor:[UIColor grayColor]];  
  •     text.borderStyle = UITextBorderStyleRoundedRect;  

  •     text.placeholder = @"";  
  •     [self.view addSubview:text];  

  • //戻るボタンを追加
  •       

  •     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  •     button.frame = CGRectMake(125, 40, 75, 35);  

  •     [button addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];  
  •     //[button setBackgroundColor:[UIColor grayColor]];  

  • [button setTitle:@「戻る」forState:UIcontrolStateNormal];  
  •       

  •     [self.view addSubview:button];  
  • }  

  •   
  • -(void)dismissKeyboard:(id)sender{  

  •     [text resignFirstResponder];  
  • }  
  • // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        //  
        UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        backView.image = [UIImage imageNamed:@"small3.png"];
        
        backView.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTouch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
        [backView addGestureRecognizer:singleTouch];
        
        backView.tag = 110;
        [self.view addSubview:backView];
        
        //  uitextfield
        text = [[UITextField alloc] initWithFrame:CGRectMake(30, 150, 250, 31)];
        //[text setBackgroundColor:[UIColor grayColor]];
        text.borderStyle = UITextBorderStyleRoundedRect;
        text.placeholder = @"";
        [self.view addSubview:text];
        //  
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(125, 40, 75, 35);
        [button addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];
        //[button setBackgroundColor:[UIColor grayColor]];
        [button setTitle:@" " forState:UIControlStateNormal];
        
        [self.view addSubview:button];
    }
    
    -(void)dismissKeyboard:(id)sender{
        [text resignFirstResponder];
    }
    

    3つ目の方法は、xibファイルでxibファイルのobjectsプロパティを変更し、デフォルトはviewプロパティです.UIcontrolプロパティに変更できます.xibファイルに対応するtouch downイベントからです.この方法は、xibがなければ悲劇になるという欠点がある.しかし、本来なら動的に設定できるはずですが、今のところ方法が見つからないので、そのネットユーザーが知っているなら、教えてください.
    設定はここを参照してください.
    objectsをコントロールなしに設定すると、touch downイベントに直接対応できます.
    以上の3つの方法を総合して、1つの例を編纂して、みんなはコードをダウンロードして見ることができます
    詳細:http://blog.csdn.net/ugg/article/details/7246164