how can I dismiss keyboard when I will just touch anywhere except UITextField?

后端 未结 10 1997
独厮守ぢ
独厮守ぢ 2021-02-19 11:12

Hello I have two UITextFields in my application, and want to dismiss the keyboard when I just touch anywhere except the UITextFields how can I do this?

10条回答
  •  悲&欢浪女
    2021-02-19 11:51

    You need to create a custom button at the background and then connect it to the IBAction method that calls resignFirstResponder on the your UITextField

    something like this enter image description here

    Edit:

    as you want to add the button programmatically than you can use this code

    - (void)viewDidLoad
    {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
        button.buttonType = UIButtonTypeCustom;
        [button addTarget:self action:(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        [button release];
    

    and than add your controller code after this.

提交回复
热议问题