0
votes

I'm trying to check if a dictionary that is passed to a ViewController has a link to an image. If it has a link, I want to show the pic, using AlamofireImage.

If not, then simply remove the UIImageView from the scrollView, so the other items below the image would stick to the top of the screen.

Here's the setup:

UIScrollView

-> UIView

--> UIImageView

--> UILabel (some short text for title)

--> UILabel (some long text for content that makes the view scrollable)


I've setup my views through Interface Builder and Auto Layout like so:

UIImageView: top, left, right, bottom constraints with a 1:1 aspect ratio of height:width no particular height/width

UILabel: top (required priority) image, top (high priority) top-layout-guide, right, left, bottom constraints

2nd UILabel: top, left, right, bottom constraints


Inside ViewDidLoad

if let imageURLString = item["imageLink"] as? String {
    let imageURL = NSURL(string: imageURLString)!
    image.af_setImageWithURL(imageURL, placeholderImage: nil, filter: nil, imageTransition: .CrossDissolve(2), runImageTransitionIfCached: true, completion: nil)
}
else {
    self.image.removeFromSuperview()
    self.scrollView.setNeedsLayout()
}

With this code, when there is a link, it loads the image and everything is displayed well without messing the scrollView. But removing it from scrollView prevents the scrollView to be able to scroll. Even though the scroll bar indicator shows up and the view "tries" to scroll, but everything gets frozen!

2

2 Answers

0
votes

In the auto layout environment it is not good to add or remove view's programatically, if it is necessary then you must take care of the respective constraints. In your case, what is going wrong is, you have set your first label's top space to the imageView above it and you are completely removing that imageView inside viewDidLoad as per the requirement which is not correct way of dealing with auto layout.

What i can suggest you is do not remove your imageView from its superView and instead just set height constraint to the imageView and set its initial height constraint constant to 0, so you will not able to see imageView now. Just update the constraits and create outlet of the height constraint and change your code as below -

if let imageURLString = item["imageLink"] as? String {
  let imageURL = NSURL(string: imageURLString)!
  image.af_setImageWithURL(imageURL, placeholderImage: nil, filter:  nil, imageTransition: .CrossDissolve(2), runImageTransitionIfCached:  true, completion: nil)
  imageViewHeightConstraint.constant = <heightYouWantToSet>
  self.view.setLayoutIfNeeded()
} else {
  imageViewHeightConstraint.constant = 0 //Hide imageView by setting its height to 0
  self.view.setLayoutIfNeeded()
}

You might also need to change priority of your imageView height constraint to Low to prevent breaking the constraint

enter image description here

1
votes

Your UIScrollView gets "frozen" because its content view is no longer bigger than the scroll view itself. This is because when you take a view out of a scroll view, you need to add new constraints so that Auto-Layout can recalculate the size of the content view.