简体   繁体   中英

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?

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

Transformations can not be used along with constraints, probably you may receive some runtime warnings related to constraints if you use the code above.

Use either of the way, just add view as subview programatically in view and apply transformations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM