Lessons learned from eaGeier #7

Creating a Done Button

As described before, the eaGeier needs a doneButton e.g. to close the date picker.

As it is needed on several occasions, it is best to create it lazily in its getter method. To do that create a custom button, set its frame and its font. To get the “button-style feeling” set the background using a gradient image. Setting its alpha to 0.8 shows what is below but still keeps the text of the button readable. And finally set the action which should be fired when the the button is touched:

- (UIButton *)doneButton {
    if (!_doneButton) {
        _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _doneButton.frame = CGRectMake(0, 0, 200, 40);
        _doneButton.titleLabel.font = [UIFont boldSystemFontOfSize:24];
        _doneButton.backgroundColor = [[UIColor colorWithPatternImage:
                                       [UIImage imageNamed:@"keyboardButtonGradient.png"]]
        [_doneButton setTitle:NSLocalizedString(@"Done", @"KeyBoardDoneButton")
        [_doneButton addTarget:self 
    return _doneButton;

To actually close the keyboard, just resign the first responder status of the field:

- (IBAction)keyBoardShouldClose:(id)sender
    [self.currentFirstResponder resignFirstResponder];

Leave a Reply

Your email address will not be published.