Having trouble launching with scrollview in 'Closed' state

42 Views Asked by At

I am using a scrollView with two containers in order to add a slide out menu to a tabbed app that I am working on.

When the app is launched, if the user is already logged in, the main page is presented and the left slide menu is initially closed. However, if the user is not logged in and has to get to the main page via the login button, the main page is presented with the menu open. I cannot figure out how to present it closed.

In the mainViewController this is the func used to close the menu initially

func closeMenu(animated:Bool = true){
    scrollView.setContentOffset(CGPoint(x: leftMenuWidth, y: 0), animated: animated)
}

In the viewDidLoad I call the following to initially close the menu

        dispatch_async(dispatch_get_main_queue()) {
        self.closeMenu(true)
    }

I am using NSNotifications to toggle the menu open and closed via a button on the main page. So I tried to post the 'toggleMenu' notification when the login button is tapped but that does not work

PFUser.logInWithUsernameInBackground(username!, password: password!) {
            (user: PFUser?, error: NSError?) -> Void in
            if user != nil {
                print("successful login")


                dispatch_async(dispatch_get_main_queue(), { () -> Void in

                    let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ContainerHome")
                    self.presentViewController(viewController, animated: true, completion: nil)

                    NSNotificationCenter.defaultCenter().postNotificationName("toggleMenu", object: nil)
                })
}

I am a beginner, so if i left out some details I do apologize.

Thanks in advance for any help given.

1

There are 1 best solutions below

1
On BEST ANSWER

First, you don't need to switch to the main queue in your viewDidLoad() method. It will always be called in the main thread (at least by the OS).

You don't want to do any messing with sizes, frames, offsets, etc in the viewDidLoad() method. The view hasn't yet been added to the superview, so its size and position is meaningless at this point.

Try calling closeMenu() from the viewWillAppear() method instead. It may still not work, but it's closer to the point you want your code to run. If it doesn't work directly from there, you can delay it until after the view has updated with something like this:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.01 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
        self.closeMenu(false)
    }
}

Or, you can play with adding the call to your override of viewDidLayoutSubviews():

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    if menuShouldBeClosed {
        closeMenu(false)
    }
}

menuShouldBeClosed is a Boolean property, because you'll need some way to keep track here.