3

I am currently trying to access the height of a view which I previously set anchors for. So for example, I set the left, right, top, and bottom anchors of searchTable using the following in my ViewController:

searchTable.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
searchTable.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
searchTable.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
searchTable.bottomAnchor.constraint(equalTo: menuBar.topAnchor).isActive = true

searchTable is an object of a class that I created that inherits from UIView and has a UIImageView in it. I constrain the UIImageView by using the following in the init function:

imageView.topAnchor.constraint(equalTo: topContainer.topAnchor, constant: 10).isActive = true
imageView.leftAnchor.constraint(equalTo: topContainer.leftAnchor, constant: 10).isActive = true
imageView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: topContainerMultiplier * imageProfileMultiplier).isActive = true
imageView.widthAnchor.constraint(equalTo: self.heightAnchor, multiplier: topContainerMultiplier * imageProfileMultiplier).isActive = true

where:

let topContainerMultipler: Double = 1 / 7
let imageProfileMultipler: Double = 1 / 5

Inside the init function of searchTable, I try want to be able to set the corner radius to be half the image size. I tried to use self.frame.height, self.frame.size.height, self.bounds.height and also getting the .constant value of the self.heightAnchor constraint, but all returns 0.

I would think that there is a solution to get around this, but I haven't been able to find it.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
kbunarjo
  • 1,277
  • 2
  • 11
  • 27
  • And what about view1.frame.bounds? And are you sure that you are trying to print the frame right when the view is reloaded? Put some code please to show when you are trying to get the height – Woof Oct 03 '17 at 07:32
  • You should be able to get it a few ms after the view was presented with: Timer.scheduledTimer(withTimeInterval: 0.01, repeats: false) { (nil) in print(self.imageView.frame) } – eharo2 Jul 27 '18 at 22:35

2 Answers2

9

You are querying the height of your view's frame after defining your constraints, but before layout has actually resolved those constraints into a frame rectangle. One approach would be to call layoutIfNeeded to force layout to happen immediately, then use view.frame.size.height. The init method is probably not an appropriate place to do this, as opposed to perhaps inside viewDidLoad on the controller. Or you might do this in viewDidLayoutSubviews to recalculate the corner radius every time your view's layout is updated (in which case you won't need layoutIfNeeded).

4bar
  • 531
  • 2
  • 14
  • I tried to access the height in a function which executes when a button is pressed. However, when I debug inside the function, frame.height is still 0. – kbunarjo Oct 03 '17 at 08:42
  • When you hit the button your image view is already loaded and visible and laid out where you expect it? – 4bar Oct 03 '17 at 09:27
0

Don't use a height constraint if you already use top, bottom, leading, and trailing.

Try to visually debug your view by adding a background color, or using the debug view hierarchy button.

To get the height of a view you should use view.frame.size.height

EDIT (OP question edited)

Your problem is that you try to access the object height when it's just initialized but not displayed yet. You should create a function that update your UI and call it after your view is loaded.

Damien
  • 3,322
  • 3
  • 19
  • 29
  • Thanks for the answer Damien, but it wasn't what I was looking for. It was my fault since I may have been a little unclear about my problem. I updated my question with some more information + code! – kbunarjo Oct 03 '17 at 08:00
  • I think your problem is that you try to get the height too soon. I edited my answer – Damien Oct 03 '17 at 08:20
  • I tried to get the heights using a function that is called when a button is pressed. However, it is still giving me 0 as the height. Do you know how I can get around this? – kbunarjo Oct 03 '17 at 20:07
  • If the height has been set by constraints the view.frame.size.height will be 0 – jcubero Jul 01 '20 at 23:25