user1923975
user1923975

Reputation: 1389

ViewWillAppear not executing code

I've got this block of code in my ViewWillAppear method which simply moves a set of labels down off the screen:

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

    //Hide 1,3,5 RM labels
    oneRepMax.center.y += view.bounds.height
    threeRepMax.center.y += view.bounds.height
    fiveRepMax.center.y += view.bounds.height

}

All 3 labels have been properly linked to my storyboard file. When I run the project, nothing happens.

If I copy the exact same code into a method linked to a button, it works as expected.

There isn't anything else in the project so i'm puzzled as to why this doesn't work.

I'm using Xcode 7 and following a tutorial where this is working in the ViewWillAppearMethod. as follows:

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

    heading.center.x -= view.bounds.width
    username.center.x -= view.bounds.width
    password.center.x -= view.bounds.width
    loginButton.center.y += 30
    loginButton.alpha = 0



}

Upvotes: 0

Views: 1759

Answers (4)

dede.exe
dede.exe

Reputation: 1310

Try to change your code to viewDidAppear.

You can find more information in this old post.

Upvotes: 0

Alejandro Iván
Alejandro Iván

Reputation: 4061

There are several methods called when you load a View controller. Their order is something like this (on UIViewController):

loadView

viewDidLoad

viewWillAppear:

viewWillLayoutSubviews

layoutSubviews (on UIViews inside the view controller)

viewDidLayoutSubviews (here the view controller knows everything is set)

viewDidAppear:

From viewDidLayoutSubviews you're completely sure your changes will take effect, because elements are set. Before it, you can't be sure (unless you cached the view controller and it is appearing for a second time).

Upvotes: 0

pulp
pulp

Reputation: 1828

This chunk of code is from Ray Wenderlich book iOS Animation by Tutorials, right? I had the same problem. I found the cause of this problem - the view from the tutorial don't use the Auto Layout and Size Classes, that's why a label don't respond on setting its coordinates. Try to change the color of the label in viewWillAppear method like labelText.textColor = UIColor.greenColor() and everything goes ok, but if you try to set coordinates like labelText.center.x -= view.bounds.width nothing happens. So click your view and go to File Inspector, scroll a bit and you'll see the checkmarks Use Auto Layout and Use Size Classes uncheck it and you'll get what you need. But you'll get pain in the ass either because this is a different and long story to code without Auto Layout. good luck then.

Upvotes: 2

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

You never know the bounds untill the all the labels appear on the screen.

Try to do the same in viewDidAppear method.

From Documentation:

This method is called after the completion of any drawing and animations involved in the initial appearance of the view. You can override this method to perform tasks appropriate for that time, such as work that should not interfere with the presentation animation, or starting an animation that you want to begin after the view appears.

Upvotes: 2

Related Questions