Hide the keyboard when you touch outside of a UITextField

Discussion in 'iOS Programming' started by winstonzeddemor, Nov 5, 2010.

  1. winstonzeddemor macrumors newbie

    Joined:
    Jan 8, 2010
    #1
    I have searched many times for this and there does not seem to be a good thread with the answers in it, so here is how I did it. I hope it helps someone.

    In the UITextFieldDelegate header file, create a pointer to, among other things, a UIButton:

    Code:
    ..
    
    //a view
    UIView *myView;
    
    //some UITextFields
    UITextField field1;
    UITextField field2;
    UITextField field3;
    UITextField field4;
    
    //a button
    UIButton *releaseButton;
    
    ..
    
    First, re-implement the textFieldShouldBeginEditing function. Bring the UITextField that is being edited to the front and add a semi-transparent button behind it, this way any touch events outside of the UITextField will be received by the new button:

    Code:
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
            //bring the text field to the front
    	[myView bringSubviewToFront:textField]; 
    
            //create a custom button
    	releaseButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    
            // make it the size of the screen
    	releaseButton.frame = CGRectMake(0, 0, 320, 480); 
    
            //give it a tag that you can use later
    	releaseButton.tag = 999;  
    
            //it needs no title
    	[releaseButton setTitle:@"" forState:UIControlStateNormal];  
    
            //set it to black
    	releaseButton.backgroundColor = [UIColor blackColor];  
    
            //now it will appear to darken the rest of the view
    	releaseButton.alpha = 0.6; 
    
            //make it call buttonPressed when you touch it
    	[releaseButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];  
    
    	// add it to myView one object beneath the current text field
    	[myView insertSubview:releaseButton belowSubview:textField];
    
    	return YES;
    }
    
    The buttonPressed function should look for the '999' tag that is assigned to the new button, and then resign some first responders. At this point, I just had the button resign all of the first responders for all of the text fields in the view:

    Code:
    -(IBAction)buttonPressed:(id)sender{
    	
    	if ([sender isKindOfClass:[UIButton class]]){
    
    		int myTag = [(UIButton *)sender tag];
    
    		if(myTag == 999){
    
                            //resign all of the text fields first responders
    			[field1 resignFirstResponder];
    			[field2 resignFirstResponder];
    			[field3 resignFirstResponder];
    			[field4 resignFirstResponder];
    
                            //hide and release the button
                            if(releaseButton != nil){
    
    			    releaseButton.hidden=YES;
    
    			    [releaseButton removeFromSuperview];
    
                                releaseButton=nil;
    
                            }
    
    		}
    
    	}
    }
    
    The textFieldShouldReturn function must also be altered, so that when a user presses the "Done" key on the keyboard, the releaseButton is hidden and released.

    Code:
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    	if(releaseButton != nil){
    
    		releaseButton.hidden=YES;
    
    		[releaseButton removeFromSuperview];
    
    		releaseButton=nil;
    
    	}
    
    	[textField resignFirstResponder];
    
    	return YES;
    }
    
    That works for my app, but the user cannot select another UITextField without deselecting the current one, which can get annoying...but I guess
    UITextFields are already pretty annoying.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I'd say so!

    Just so you know, it is possible to do this without requiring deselecting the current one. I know because that's what my app, a.k.a., does! Let me know if you want more details.
     
  3. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    Why don't you open your source?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Open my source for what? Please don't say my whole app.
     
  5. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #5
    No, not your whole App, just to solve this question:D
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Attached is a sample project that shows three text fields and uses a background button to dismiss the keyboard. It also demonstrates "tabbing" between fields via the use of the 'return' button; only the last field has a Done button to dismiss the keyboard. Enjoy!
     

    Attached Files:

Share This Page