Komal K Komal K - 1 year ago 147
iOS Question

get Height Value from NSLayoutConstraint

In my project I am using AutoLayOut, There is one UIView which is subView main View, I have set its width(using constrains) equal to 2 :3 superView's width and its height equal to its View. I need view to be circular shape so I am setting its cornerRadius to heightOfView/2. There is 1:1 aspect ration of views's height and width. I have created Outlet of that constrain as

@IBOutlet weak var circleHeight: NSLayoutConstraint

Now I want to acces its Height or Width but I m not gwtting actual value,


I am getting value which I have stored from Storyboard, There Is something I am Missing but could not figured it out

enter image description here
Selected View should be circular. but when i print its constain's values it gives height and width as 214 only

Answer Source
  1. I think you might be fetching value in viewWillAppear or ViewDidLoad but views frames don't get updated when these method gets called
  2. So you need to fetch the width or height in ViewDidAppear method.
  3. If get the height or width in ViewDidLayoutSubViews, performance gets degraded as it gets called many times.

Here is Code

override func viewDidAppear(animated: Bool){
       let viewHeight = CGRectGetHeight(yourView.frame)
       yourView.layer.cornerRadius = viewHeight / 2
       yourView.layer.masksToBounds = true
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download