I have been following a tutorial on how to implement a login with parse with the function of logging in with Facebook and Twitter, however, I find a detail.
In principle, the elements of the main view are hidden by means of the function viewDidLayoutSubviews
, that is, they are sent to the bottom of the screen and then move up in the form of animation, this animation is executed in the function viewDidAppear
.
So far everything is perfect, however when I select one of those textFields
that are part of the view to enter a text, obviously the keyboard appears and in theory I should move the elements up so as not to hide them when writing, here I suppose that it is activates the function again viewDidLayoutSubviews
and executes the code snippet that hides me all the elements at the bottom as it is done when the application loads to make the animation possible.
How can I prevent the elements from disappearing every time I try to write to any of the textFields
?
override func viewDidLoad() {
super.viewDidLoad()
// create an array of all the views we want to animate in when we launch the screen
viewsToAnimate = [self.logInView?.usernameField, self.logInView?.passwordField, self.logInView?.logInButton, self.logInView?.passwordForgottenButton, self.logInView?.facebookButton, self.logInView?.signUpButton, self.logInView?.logo]
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// We to position all the views off the bottom of the screen
// and then make them rise back to where they should be
// so we track their final position in an array
// but change their frame so they are shifted downwards off the screen
viewsFinalYPosition = [CGFloat]();
for viewToAnimate in viewsToAnimate {
//print(viewToAnimate.frame)
let currentFrame = viewToAnimate.frame
print(viewsFinalYPosition.count)
viewsFinalYPosition.append(currentFrame.origin.y)
viewToAnimate.frame = CGRectMake(currentFrame.origin.x, self.view.frame.height + currentFrame.origin.y, currentFrame.width, currentFrame.height)
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// Now we'll animate all our views back into view
// and, using the final position we stored, we'll
// reset them to where they should be
if viewsFinalYPosition.count == self.viewsToAnimate.count {
UIView.animateWithDuration(1, delay: 0.0, options: .CurveEaseInOut, animations: { () -> Void in
for viewToAnimate in self.viewsToAnimate {
print(viewToAnimate.frame)
let currentFrame = viewToAnimate.frame
print(self.viewsFinalYPosition.count)
viewToAnimate.frame = CGRectMake(currentFrame.origin.x, self.viewsFinalYPosition.removeAtIndex(0), currentFrame.width, currentFrame.height)
}
}, completion: nil)
}
}
https://dl.dropboxusercontent.com/u/4915071/viewsDissapear.mov
Give them the starting position (hidden) in the
viewDidLoad
or add a boolean property:So that you
viewDidLayoutSubviews
put it intrue
when the elements have already been hidden:I think something like this might work for you.