regrecall
regrecall

Reputation: 521

IOS How to make the view cover keyboard

I encounter the a problem:

I have a view controller like this. enter image description here

TO make tool bar up when the keyboard appear, I move the self.view to up.

[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + keyboardFrame.size.height * (up ? -1 : 1), self.view.frame.size.width, self.view.frame.size.height)];

Now I want to click the left button in the tool bar, and appear the view that frame is same as the this keyboard.

But where can I add this view? if add the subview to the self.view, it will move up with the self.view on the top of the keyboard, not cover. I'm a beginner about IOS, I have no idea about it, and have searched, but got nothing about this.

another question, IF when the toolbar at the bottom, I aslo want to click the left button on it to show the view(the animation and frame both are the same as the keyboard), how can I do ?

Can you help me? Thanks

Upvotes: 0

Views: 1148

Answers (2)

Fab1n
Fab1n

Reputation: 2284

Am I right, that you want to pop the bar above the keyboard attached up and down with the keyboard?

Then you have to register to keyboard show and hide notifications and animate the view up and down based on the notifications and the keyboard show and hide speed.

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification 
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillHide:) 
                                             name:UIKeyboardWillHideNotification 
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification object:nil];

Now you have to implement all the selector methods:

//Code from Brett Schumann
-(void) keyboardWillShow:(NSNotification *)note{
    // get keyboard size and loctaion
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];

    // get a rect for the textView frame
    CGRect containerFrame = containerView.frame;
    containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];

    // set views with new info
    containerView.frame = containerFrame;

    // commit animations
    [UIView commitAnimations];

    [self showSendButton];
}

-(void) keyboardWillHide:(NSNotification *)note{
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    // get a rect for the textView frame
    CGRect containerFrame = containerView.frame;
    containerFrame.origin.y = self.view.bounds.size.height - containerFrame.size.height;

    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];

    // set views with new info
    containerView.frame = containerFrame;

    // commit animations
    [UIView commitAnimations];

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, containerView.frame.size.height-46.0f+20.0f, 0);
    _table.contentInset = contentInsets;
    _table.scrollIndicatorInsets = contentInsets;

    //display button based on facebook login status
    if ([facebookService.facebook isSessionValid]) {
        [commentButton makeButtonLogoutButton];
        textView.editable = YES;
    } else {
        [commentButton makeButtonLoginButton];
        textView.editable = NO;
    }
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+20.0f, 0.0);
    _table.contentInset = contentInsets;
    _table.scrollIndicatorInsets = contentInsets;
}

Surly replace the given view on top of the keyboard with your own view. This view should be a single view containing all the buttons and stuff.

Greetings

Upvotes: 1

Andrei Chevozerov
Andrei Chevozerov

Reputation: 1029

First of all, why do you want to cover the keyboard? It looks like not the very good idea to do so. And I don't think it's even possible at all. For the answer to your second question you should look at UIView documentation:

[UIView animateWithDuration:0.3 animations:^{
    // place your view wherever you want
    CGRect frame = myView.frame;
    frame.y = self.view.frame.size.height - frame.size.height;
    myView.frame = frame;
}];

Upvotes: 0

Related Questions