Itzik984
Itzik984

Reputation: 16774

Stop affine transform from applying on subview

I have a UIView that holds a UILabel inside.

After applying affine transform on the UIView using:

myView.transform = CGAffineTransformMakeScale(4, 4);

My UILabel (which is a sub view to myView) grows as well.

Is there a way to prevent this?

i tried:

1) Using the CGAffineTransformIdentity flag on the label.

2) Adding a superview to myView and adding myView as superview's subview, and the label as a subview to the superview (and not myView).

Non of them seem to be working, the label keeps growing.

Any ideas?

Upvotes: 1

Views: 260

Answers (1)

bbarnhart
bbarnhart

Reputation: 6710

You answered your own question with option 2. Not sure why it's not working since you did not supply any code. The playground code below shows it will work. Uncomment out the last line to transform the subview but not the label.

import UIKit
import XCPlayground

let superview = UIView(frame: CGRect(x: 10, y: 10, width: 200, height: 200))
XCPlaygroundPage.currentPage.liveView = superview
superview.backgroundColor = UIColor.redColor()

let view = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
view.backgroundColor = UIColor.greenColor()
superview.addSubview(view)

let label = UILabel(frame: CGRect(x: 20, y: 10, width: 40, height: 40))
label.text = "Hello"
superview.addSubview(label)

//view.transform = CGAffineTransformMakeScale(2, 2)

Upvotes: 1

Related Questions