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"]]
                                      colorWithAlphaComponent:0.8];
        [_doneButton setTitle:NSLocalizedString(@"Done", @"KeyBoardDoneButton")
                    forState:UIControlStateNormal];
        [_doneButton addTarget:self 
                        action:@selector(keyBoardShouldClose:) 
              forControlEvents:UIControlEventTouchUpInside];
    }
    return _doneButton;
}

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

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

Leave a Reply

Your email address will not be published.