Search code examples
iosswiftuiviewconstraintsnslayoutconstraint

Swift constrain view to other view that has been transformed


I have a highlightView which I would like to constraint to another view. This is my function for it:

func showHighlightView(viewToHighlight: UIView, height: CGFloat) {
    self.view.addSubview(highlightView)
    highlightView.heightAnchor.constraint(equalTo: viewToHighlight.heightAnchor).isActive = true
    highlightView.widthAnchor.constraint(equalTo: highlightView.heightAnchor).isActive = true
    
    highlightView.centerXAnchor.constraint(equalTo: viewToHighlight.centerXAnchor).isActive = true
    highlightView.centerYAnchor.constraint(equalTo: viewToHighlight.centerYAnchor).isActive = true
    highlightView.layer.cornerRadius = height/2

    highlightView.layer.add(self.scaleAnimation, forKey: "scale")

    self.view.bringSubviewToFront(viewToHighlight)
}

This is working for most of my cases. However I have one view which I transform like this:

var transformerBumbleBee = CGAffineTransform.identity
transformerBumbleBee = transformerBumbleBee.translatedBy(x: 25, y: -80)
transformerBumbleBee = transformerBumbleBee.scaledBy(x: 1, y: 1)
self.addListButton.transform = transformerBumbleBee

with this addListButton my showHightLightView() is constraining to the identity-constraint of addListButton and not the transformed. Is there a way to change that?


Solution

  • transform doesn't apply constraints to other views , you need to make translate and scale actions with changing the constraints's constants/multipliers values