Toolbar is animating when multitasking
Asked Answered
H

1

6

So after my long search via google and SO, I have not found a similar issue. Some came close but not like mine.

Issue at hand: In my app (WKWebView based) if the keyboard is shown, and I double tap home and switch to another app which the keyboard is shown, my apps keyboard is hidden. Not an issue. So when I multitask again, in the snapshot it takes of my app, the keyboard is gone, but the custom toolbar I have above it seemed to remain in place with no keyboard below. I tap my app and it animates up and then back down to the bottom of the screen.

What I've tried: I know about the [self.view endEditing:YES], resigningFirstResponder and all the methods to close the keyboard. Tried placing and mixing through the viewWillDisappear, applicationWillResignActive and applicationDidEnterBackground. None of which will handle my toolbar issue.

This is how I'm animating my keyboard on and off the screen, while keeping the toolbar visible.

    - (void)keyboardWillShow:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey];
    NSValue *endFrame = info[UIKeyboardFrameEndUserInfoKey];

    [UIView animateWithDuration:durationValue.doubleValue
                          delay:0
                        options:(curveValue.intValue << 16)
                     animations:^{
                         self.toolBar.frame = CGRectMake(0,
                                                         [endFrame CGRectValue].origin.y - self.toolBar.bounds.size.height+44,
                                                         self.toolBar.bounds.size.width,
                                                         self.toolBar.bounds.size.height);
                     }
                     completion:nil];
}

- (void)keyboardWillHide:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey];
    NSValue *endFrame = info[UIKeyboardFrameEndUserInfoKey];

    [UIView animateWithDuration:durationValue.doubleValue
                          delay:0
                        options:(curveValue.intValue << 16)
                     animations:^{
                         self.toolBar.frame = CGRectMake(0,
                                                         [endFrame CGRectValue].origin.y - self.toolBar.bounds.size.height,
                                                         self.toolBar.bounds.size.width,
                                                         self.toolBar.bounds.size.height);
                     }
                     completion:nil];
}

Does anybody have a clue or thought?

Again my app is WKWebView based, so directly calling or directing towards textviews, trying to focus in the webview is pointless.

If anybody needs some more code, just let me know. Any help would be appreciated. Thank You.

UPDATED 1/20/18

So here is the requested code pertaining to where and how my toolbar gets initialized and loaded.

h.

UIToolbar *toolBar;
@property (nonatomic, strong) UIToolbar *toolBar;

m.

@synthesize toolBar;

- (void)viewDidLoad {
    //Toolbar setup
    CGRect toolFrame, remain;
    CGRectDivide(self.view.bounds, &toolFrame, &remain, 48, CGRectMaxYEdge);
    self.toolBar = [[UIToolbar alloc] initWithFrame:toolFrame];
    [self.toolBar setBarStyle:UIBarStyleBlackTranslucent];
    [self.toolBar setClipsToBounds:YES];
    [self.toolBar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
    [self.view addSubview:self.toolBar];
}

That's about it, other then some code calling to refresh it when I switch buttons, nothing else is tied to this toolbar.

This doesn't get called unless I tap a switch or need to refresh it when the user changes the background, Dealt with NSUserDefaults in another view.

-(void)viewWillLayoutSubviews{
    [self.currentScrollView layoutIfNeeded];
    [self.toolBar layoutIfNeeded];
    [self.view layoutSubviews];
}
Hydroponics answered 9/2, 2017 at 0:35 Comment(8)
This is still a persistent issue with my app.Hydroponics
Where did you create the toolBar and how?Funderburk
It’s created in my main view (viewDidLoad) once the app loads. I created it in the h file with property and then synthesizing in the m file and adding it to the subView.Hydroponics
Can you show the code?Funderburk
I’ll update my question with the relevant code when I get home from work.Hydroponics
Post has been updated.Hydroponics
Don't you want to hide keyboard when application goes to background? that would probably be an easy fix?Ebonyeboracum
They keyboard does hide. It’s the toolbar above it that gets stuck as if the keyboard was visible.Hydroponics
H
0

So below is what is working for me.

In my AppDelegate.m I simply put endEditing:

- (void)applicationWillResignActive:(UIApplication *)application {
    [self.window endEditing:YES];
}

Now in my keyboardWillShow notification :

    - (void)keyboardWillShow:(NSNotification*)notification
{
    /////New added method//////
    if([UIApplication sharedApplication].applicationState != UIApplicationStateActive){
        return;
    }


    NSDictionary* info = [notification userInfo];
    NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey];
    NSValue *endFrame = info[UIKeyboardFrameEndUserInfoKey];

    [UIView animateWithDuration:durationValue.doubleValue
                          delay:0
                        options:(curveValue.integerValue << 16)
                     animations:^{
                         self.toolBar.frame = CGRectMake(0,
                                                         [endFrame CGRectValue].origin.y - self.toolBar.bounds.size.height+44,
                                                         self.toolBar.bounds.size.width,
                                                         self.toolBar.bounds.size.height);
                     }
                     completion:nil];    
}

That was it, something so simple after all. Just hiding the keyboard, like I did before, and checking to see if the app was active.

Now when I double tap home, the keyboard hides. If I go to another app like Messages, draw up the keyboard, double tap again and go back to my app, the keyboard never moved. Solving my issue.

Hydroponics answered 3/2, 2018 at 22:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.