1

I have a view, which shall move up like a drawer from the bottom of the screen. But it does not do anything. It just sits there =) Can anyone please tell me, why it is doing that?

This is my code:

import UIKit

class InfoPopUpVC: UIViewController {
  var superView: UIView!
  var labelText: String!
  let textLabel =  UILabel()
  let height = CGFloat(80)


  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    UIView.animateWithDuration(0.4, animations: { () -> Void in
      self.view.center.y = 50
    })
  }

override func viewDidLoad() {
  super.viewDidLoad()
  setupTextLabel()
  view.frame = CGRectMake(0, superView.frame.maxY-height, superView.frame.width, height)
  AnimationHelper.blurBackgroundForView(view)
  view.backgroundColor = .greenColor()
  }

func setupTextLabel(){
  textLabel.text = labelText
  textLabel.frame = CGRectMake(0, 0, view.frame.width, view.frame.height)
  textLabel.numberOfLines = 3
  textLabel.textAlignment = .Center

  textLabel.frame.inset(dx: 10, dy: 8)
  textLabel.sizeToFit()
  textLabel.font = UIFont(name: "HelveticaNeue-Light", size: 17)
  textLabel.textColor = .whiteColor()
  view.addSubview(textLabel)
  }
}
gutenmorgenuhu
  • 2,312
  • 1
  • 19
  • 33
  • Are you using autolayout? If so, changing centers and frames won't move views. – Jack May 21 '15 at 20:30
  • The InfoPopUpVC is called programmatically whenever I want a PopUp from the bottom to show some information. The viewController the PopUp is added as subview uses autolayout, but because it is called programmatically, I cannot set any constraints – gutenmorgenuhu May 21 '15 at 20:33
  • 1
    It's not true that you can't set constraints programmatically. There is a full set of APIs for adding constraints programmatically, and a lot of literature documenting how to do it. Read the NSLayoutConstraint class reference in Xcode for an introduction (in particular the section on creating constraints.) – Duncan C May 21 '15 at 22:13

2 Answers2

1

Try to put your code as follow inside viewDidAppear or viewWillAppear and with dispatch async. Otherwise your animation might not work.

 override func viewDidAppear(animated: Bool) {
dispatch_async(dispatch_get_main_queue(), {
            UIView.animateWithDuration(0.4, animations: { () -> Void in
      self.view.center.y = 50
    })
        })
}
kmarin
  • 332
  • 3
  • 8
  • This doesn't make any sense. – Duncan C May 21 '15 at 22:10
  • it does make sense, unfortunately, when the view are initialized the main queue is blocked and your animation might not get executed, basically with dispatch async on the main queue will make sure your animation is animated. Also he is trying to call animation the in view will appear, means view has not appeared yet, you can see the animation after views are appeared, which mean it has to be called in viewDidAppear, you might not need dispatch async, however in some case, it is required to put the animation in dispatch async on the main queue. – kmarin May 21 '15 at 22:12
  • One more thing there is also viewDidLayoutSubviews() which is called after viewDidAppear function, it will reset to default (storyboard constraints) all the views inside your main view of your view controller, this might also be another reason for animation not working. – kmarin May 21 '15 at 22:26
0

You cannot animate a frame or similar property, if the UIViewis constrained using autolayout.

You have two options:

  1. Get rid of autolayout and animate the frames Directory
  2. Use autolayout and animate the constraints (e.g. via outlets)

See the following links for examples:

How do I animate constraint-changes

IOS: ANIMATING AUTOLAYOUT CONSTRAINTS

Community
  • 1
  • 1
gutenmorgenuhu
  • 2,312
  • 1
  • 19
  • 33