0

I have a constraint to set the height of the view.

The constraint is proportional height constraint.

I want to calculate the height using the constraint that's I want to know what is the view height.

any APIs to do that ?

I know I can manually find it myself as well using formula but I don't want to do that

Lets say the height constraint 58% of parent view and it has constant value of 10 then what is the total height assigned by constraint to the view

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dan Ram
  • 171
  • 1
  • 14

1 Answers1

2

The height of the view will be:

0.58 * parentView.frame.height + 10

The height of the parentView isn't established until Auto Layout runs, so if you were to do this calculation, you would do it in an override of viewDidLayoutSubviews.

Since you aren't interested in calculating that yourself, you can simply read the final frame height of the view in an override of viewDidLayoutSubviews which runs after Auto Layout has applied the constraints.

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // read final view size here
    print(myView.frame.height)
}
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • basically I want to update some UI constraint ... so if I do it in this even ... will it trigger endless cyclic invocations – Dan Ram Sep 12 '17 at 14:10
  • What is the constraint you want to update? Perhaps there is a way to write it so that Auto Layout does the work for you. – vacawama Sep 12 '17 at 14:27
  • basically I have a view which has multiple views inside... based on the visibility of the views inside the the parent view size will be decided. so I want to decide the size ? – Dan Ram Sep 12 '17 at 15:18