0

I have Imageview and UIView on View Controller. If Imageview is nil or image is not available then UIView replace its postion.Do any know how is it possible using auto layout?

enter image description here

For trying purpose, I have fixed height and width of both(Imageview and UIView). Imageview have "top 8 pixel" and "Horizontally in container" margin. UIView have "top 0 from Imageview" and "Horizontally in container" margin. Set Imageview to nil but it doesn't work.

nadim
  • 776
  • 1
  • 12
  • 26
  • 2
    You have to do it in code, if image = nil then make the hight constraint of the image view to zero. Or you can put them in a stackview – Inder Kumar Rathore Apr 04 '17 at 12:44
  • You don't need to specify height for image. The image will be have place space what they need with compress priority. So if you don't have image the height will be 0 but if you have the height will be the image.size.height – Sergey Apr 04 '17 at 12:49
  • Thanks Inder Kumar it works – nadim Apr 04 '17 at 12:55
  • @Sergey if I don't set height it gives Constraints error "Need constraints for: Y position or height" – nadim Apr 04 '17 at 12:57
  • Yes it's warning, its normal. You can use constraint that remove in runtime to remove this warning. The interface builder have this option. – Sergey Apr 04 '17 at 13:01

3 Answers3

1

A good suggestion would be to add both of the image view and the view in a stackView and follow the steps mentioned in: UIStackView Distribution Fill Equally.

However, you can achieve what are you asking for by adding additional constraint between the bottom view and the top layout guide:

enter image description here

and then, set its priority value to be less than the default (1000) -in my example I set it to 500- and the its constant value to 0:

enter image description here

Its appearance should be displayed as dotted line, meaning that there is another constraint -with a higher priority value- deciding the x axis of the view, if this constraint has been removed/deactivated the dotted one should be activated.

Finally, make sure that if there is no available image you have to remove image view from its super view (call imageView.removeFromSuperview()), setting it as hidden or setting its alpha to 0.0 doesn't activate the dotted constraint:

class ViewController: UIViewController {
    //...

    @IBOutlet weak var imageView: UIImageView!

    // I'm checking in 'viewDidLoad' method just for describing purposes,
    // of course, you can do the check when needed...
    override func viewDidLoad() {
        // if there is something wrong, you should call:
        imageView.removeFromSuperview()
    }

    //...
}

The output would be:

enter image description here

Community
  • 1
  • 1
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
0

You can manage this by giving priority to the constraints using auto layout. Give your UIView top space from ImageView and the TopLayout. Assign a lower priority to the constraint (any value less than 1000) where you have given top space from top layout guide. If your image is nil , remove the image view from the view using imageview.RemoveFromSuperView(), the UIView will automatically take the next constraint i.e. from TopLayout guide. I am attaching a screenshot where to find the priority of the constraint on storyBoard.

enter image description here

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • Thank you for your reply, But its not working. UIView Position not changed and Imageview removed from controller. – nadim Apr 04 '17 at 13:05
0

Take the outlet of height constraint of UIImageView and do below code

if (imageview == nil){
   imageViewHeightConstraint.constant = 0
}
else{
   imageViewHeightConstraint.constant = 60
}

And other query you can ask.

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51