Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to "viewDidLayoutSubviews" that is only called once?

Tags:

ios

swift

swift3

I am searching for a alternative for viewDidLayoutSubviews that is only called once.

I use this code to move the login out of the view:

override func viewDidLayoutSubviews() {
    username_input.center.x -= view.bounds.width
    password_input.center.x -= view.bounds.width
    login_button.center.x -= view.bounds.width
}

I need to do this once, so that I can then move it in with a animation when viewDidAppear. This all worked great till I split my View into different subviews and now the function is called multiple times. I could't find any equivalent functions to this that is only called once.

like image 896
beatcraft Avatar asked Sep 15 '25 10:09

beatcraft


1 Answers

Define a boolean that turns to false the first time viewDidLayoutSubviews executes. I did this many times and it works just fine.

fileprivate var firstLayoutSubviewsTime = true

override func viewDidLayoutSubviews() {
    if firstLayoutSubviewsTime {
        firstLayoutSubviewsTime = false

        [...]
    }
}
like image 147
Tamás Sengel Avatar answered Sep 17 '25 01:09

Tamás Sengel