Search code examples
iosswiftautolayout

changing constraints of a view


I have an Image view that is surrounded by 2 UIVIEWS which are the green top bar and blue side bar.

I have coded a tap gesture [FOR hiding the views], so If you tap once, the top and side bar hide and when tapped again, they re-appear.

However, I want to code, that if the bars are visible, then the image view should hug itself to the bars and not extend, so as to prevent clipping. Please see attached pic link

But when we tap and the bars go away, I want the image view to hug the super view, how can I do that?

I've coded the gesture like so:

@IBAction func test(_ sender: UITapGestureRecognizer) {

print("single tap")

if top.isHidden == false && down.isHidden == false{
    top.isHidden = true
    down.isHidden = true

    print("first")

    
} else{
    top.isHidden = false
    down.isHidden = false
 }

image for question


Solution

  • You can try playing with anchor constraints.

    For example, put

    imageView.topAnchor.constraint(equalto: topView.bottomAnchor).isActive = true
    

    And same for side View so that they are always constraint to the image view edges.