0

I have a UITextField which animates onto the view controller (moves in from the right hand side onto the view) when I press a button. However, when I press on the UITextField or any other view, the UITextField disappears. How can I stop the UITextField from disappearing. I am using the following code:

@IBAction func joinCircleButton(sender: AnyObject) {
    let button = sender as UIButton
    joinTextField.frame.origin.x=500
    joinTextField.frame.origin.y=100
    if (button.frame.origin.x - 75>0){
    UIView.animateWithDuration(0.5, animations:{
        button.frame = CGRectMake(button.frame.origin.x - 125, button.frame.origin.y,button.frame.size.width, button.frame.size.height)
        button.transform = CGAffineTransformMakeScale(0.5, 0.5);
        self.joinTextField.frame=CGRectMake(self.joinTextField.frame.origin.x - 325, self.joinTextField.frame.origin.y,self.joinTextField.frame.size.width, self.joinTextField.frame.size.height)

    })
    }
}
override func viewDidLayoutSubviews(){
    joinTextField.center = CGPointMake(joinTextField.frame.origin.x-500, joinTextField.frame.origin.y)


}
Ell Neal
  • 6,014
  • 2
  • 29
  • 54
StevenZ
  • 6,983
  • 5
  • 16
  • 18
  • 1
    Are you using auto layout? If so, you shouldn't be setting any frames to move the button. When the view needs to redraw itself (like when you start editing your text field), the button will move back to the position defined by its constraints. – rdelmar Jan 05 '15 at 02:12
  • @rdelmar I removed the auto-layout on the button and text field, but am still using it for other things in the view? Is that a problem? – StevenZ Jan 05 '15 at 02:15
  • You can't do that if all the views are made in the same storyboard -- it's either on or off for the whole storyboard (or a whole xib file). If you don't add constraints yourself, the system adds them for you. – rdelmar Jan 05 '15 at 02:18
  • @rdelmar I have removed all constraints from the ViewController in the storyboard, but other `viewControllers` in the storyboard are using autolayout – StevenZ Jan 05 '15 at 02:21
  • As I said, you can't do that. The system will add the constraints for you. Click on one of your views, and look at the size inspector -- it will have a message saying what constraints the system will add. – rdelmar Jan 05 '15 at 02:22
  • @rdelmar Ahh...I see. How could I get around this? – StevenZ Jan 05 '15 at 02:24
  • You learn to use auto layout. Make IBOutlets to any constraints you need to modify, and modify their constant values in code to do the animation. – rdelmar Jan 05 '15 at 02:25
  • @rdelmar Do you mind if I move this conversation to chat? – StevenZ Jan 05 '15 at 02:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68192/discussion-between-rdelmar-and-stevenr). – rdelmar Jan 05 '15 at 02:30

2 Answers2

0

If you're using an iPhone simulator, try using 50 instead of 500 as the screen width is small and your text field is thus displayed off screen

joinTextField.frame.origin.x=**50**
joinTextField.frame.origin.y=100

override func viewDidLayoutSubviews(){
    joinTextField.center = CGPointMake(joinTextField.frame.origin.x-**50**, joinTextField.frame.origin.y)
}
Tony Swiftguy
  • 444
  • 3
  • 3
0

Auto layout won't let you change frame of a view. Instead, you suppose to change the constraints and call setNeedsUpdateConstraints(). If you DO need to perform an animation that temporary changes the position of the view, you can use CGAffineTransform and specifically CGAffineTransformMakeTranslate(), giving tx and ty representing "by how many points you wish to move the view". Since you also use transform to animate scale in your code, you can use CGAffineTransformConcat to "merge" the scale and transform commands into one that you can set to the view.

You're animation method should look something like this:

UIView.animateWithDuration(0.5) {
    let trans = CGAffineTransformMakeTranslation(-125, 0)
    let scale = CGAffineTransformMakeScale(0.5, 0.5)
    button.transform = CGAffineTransformConcat(trans, scale)
    self.joinTextField.transform = CGAffineTransformMakeTranslation(-325, 0)
}

Learn more about CGAffineTransform in Apple Docs

Regarding the rest of the code - you may still get the frame to check it in your if, but you have to change the part where you change the frame to using transform translate

Aviel Gross
  • 9,770
  • 3
  • 52
  • 62
  • So which method would I need to use to keep the textField from disappearing. Thank you, I'm new to IOS. – StevenZ Jan 05 '15 at 05:32
  • This is what I have: @IBAction func joinCircleButton(sender: AnyObject) { let button = sender as UIButton UIView.animateWithDuration(0.5) { let trans = CGAffineTransformMakeTranslation(-125, 0) let scale = CGAffineTransformMakeScale(0.5, 0.5) button.transform = CGAffineTransformConcat(trans, scale) self.joinTextField.transform = CGAffineTransformMakeTranslation(-25, -50) } } – StevenZ Jan 05 '15 at 05:53
  • But the textField still moves when pressed, but the button stays put. – StevenZ Jan 05 '15 at 05:54