Reputation: 836
I have a green rectangle (viewA) with a inner red rectangle (viewB). If I try to modify the dimension of the viewA the viewB is not automatically changed.
I try to:
All these solution don't work.
This is my code:
@IBOutlet weak var viewA: UIView!
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonAction(_ sender: Any) {
viewA.autoresizesSubviews = true
self.viewA.autoresizingMask = [.flexibleWidth, .flexibleHeight,.flexibleTopMargin, .flexibleLeftMargin, .flexibleRightMargin, .flexibleBottomMargin]
viewA.bounds = CGRect(x: 0, y: 0, width: 100, height: 100) // or frame instead of bounds
}
Thanks to everyone.
EDIT:
I add the two views in the storyboard. Here you can see also the constraint of the viewB.
Upvotes: 1
Views: 1092
Reputation: 8904
Don't mix up frame and auto layout of a view, both are behaving differently.
You need to do something like as follow.
Constrains for viewA
Constraints for viewB
Now you need to Create an @IBOutlet
to the constraint you need to modify(of viewA).
@IBOutlet weak var const_height_viewA: NSLayoutConstraints!
So on button click you need to change only constraint's constant.
@IBAction func buttonAction(_ sender: Any) {
const_height_viewA.constant = 250
//changing height constraint of viewA will automatically updates viewB frame.
}
Note: No need to modify constraints of viewB as it will be automatically adjusted by given constraints.
For pictorial reference
Upvotes: 1