0

I've constrained an UIImageView in IB. I tried to animate another constraint, the center, but when I'm using layoutIfNeeded with animateWithDuration, I can see that iOS is also resizing my UIImageView.

With a little help from print() I've figured out that my UIImageView is actually 1000.0, 1000.0 before super.viewDidLoad() and 200.0, 200.0 after, which is the size of my constraint.

I don't get it. I've never ever entered 1000 anywhere, and my image asset is 200x200, 400x400@2x and 600x600@3x.

What am I doing wrong ?

edit : Here's some of my code. I've tweaked it a little bit, more based on my asset size, but it's still not what I wanted :

@IBOutlet weak var bigLogoOutlet: UIImageView!

override func viewWillAppear(_ animated: Bool) {
    bigLogoOutlet.frame.size.height = 0
    bigLogoOutlet.frame.size.width = 0


}

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "My title"
}
override func viewDidLayoutSubviews() {
    UIView.animate(withDuration: 0.5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
        self.bigLogoOutlet.frame.size.height = 200
        self.bigLogoOutlet.frame.size.width = 200
        self.view.layoutIfNeeded()
        }, completion: nil)
}

That's my asset in IB

So now, It's resizing from 0x0 to 200x200. Ok, cool, but it's also moving from x:0 y:0 where my IB constraints told him to be at least superview.center.x !

petaire
  • 495
  • 1
  • 10
  • 23
  • Please post that code. Then you are likely to get answer as fast as light. – pedrouan Sep 02 '16 at 22:05
  • Possible duplicate of [Why am I having to manually set my view's frame in viewDidLoad?](http://stackoverflow.com/questions/6757018/why-am-i-having-to-manually-set-my-views-frame-in-viewdidload) – Marco Santarossa Sep 02 '16 at 22:15

1 Answers1

1

Connect your constraints from your storyboard to your code as an IBOutlet. Then in viewDidLayoutSubviews:

widthConstraint.constant = 200
heightConstraint.constant = 200
Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61