See previous post for how to add and hook up a search bar. Setup something like this.

Screen Shot 2017-06-03 at 3.55.19 PM.png

Center the label.

Screen Shot 2017-06-03 at 3.57.02 PM.png

But also give it a custom height.

Screen Shot 2017-06-03 at 3.57.29 PM.png

We are going to calculate the value and turn that constraint ON/OFF depending on whether the keyboard is displayed or not.

Drag out the two constraints from the storyboard.

Screen Shot 2017-06-03 at 3.58.30 PM.png

Calculate the keyboard height

Register yourself for the keyboard, calculate the height, and then set the calculated constraint value while turning the appropriate constraint on/off.

Screen Shot 2017-06-03 at 3.59.40 PM.png

Screen Shot 2017-06-03 at 3.59.40 PM.png

That’s it!

Jun-03-2017 16-03-36.gif

Code

ViewController.h

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UILabel *label;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelYCustomHeightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelYCenterContraint;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.searchBar.delegate = self;

    [self setupKeyboardNotification];
}

// return NO to not become first responder
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    return YES;
}

// called when text starts editing
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    // adjust layout for keyboard display
}

// return NO to not resign first responder
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    // adjust layout for no keyboar

    return YES;
}

// called when text ends editing
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    // turn off adjusted height
    self.labelYCustomHeightConstraint.active = NO;

    // turn on center Y
    self.labelYCenterContraint.active = YES;
}

// called when text changes (including clear)
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.label.text = searchText;
}

// called when keyboard search button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.label.text = @"searchButton clicked";
}

// called when cancel button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    self.label.text = @"cancelButton clicked";

}

- (IBAction)onPressed:(UIButton *)sender
{
    // turn on adjusted height
    self.labelYCustomHeightConstraint.active = YES;

    // turn off center Y
    self.labelYCenterContraint.active = NO;
}

- (IBAction)offPressed:(UIButton *)sender
{
    // turn off adjusted height
    self.labelYCustomHeightConstraint.active = NO;

    // turn on center Y
    self.labelYCenterContraint.active = YES;
}

- (IBAction)dismissPressed:(UIButton *)sender
{
    [self.searchBar resignFirstResponder];
}

#pragma mark - Keyboard notification

- (void)setupKeyboardNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
}

- (void)keyboardWillShowNotification:(NSNotification *)notification
{
    [self updateViewConstraintsForKeyboardNotification:notification];
}

- (void)updateViewConstraintsForKeyboardNotification:(NSNotification *)notification
{
    CGFloat keyboardHeight = [self keyboardHeightForNotification:notification];
    CGFloat viewHeight = self.view.bounds.size.height;
    CGFloat midPointY = (viewHeight - keyboardHeight) / 2;

    // turn on adjusted height
    self.labelYCustomHeightConstraint.constant = midPointY;
    self.labelYCustomHeightConstraint.active = YES;

    // turn off center Y
    self.labelYCenterContraint.active = NO;
}

- (CGFloat)keyboardHeightForNotification:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    NSValue *keyboardFrameBegin = [userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
    CGFloat keyboardHeight = keyboardFrameBeginRect.size.height;
    return keyboardHeight;
}


@end
Advertisements