0

I add UIViews dynamically in code. There are a lot of views I want to add. Here is one for example:

let typeImage = UIImageView()
typeImage.translatesAutoresizingMaskIntoConstraints = false
rootView.addSubview(typeImage)
typeImage.widthAnchor.constraint(equalToConstant: 28).isActive = true
typeImage.heightAnchor.constraint(equalToConstant: 28).isActive = true
typeImage.leftAnchor.constraint(equalTo: rootView.leftAnchor, constant: 4).isActive = true
typeImage.topAnchor.constraint(equalTo: rootView.topAnchor, constant: 4).isActive = true
typeImage.backgroundColor = gh.myRed
typeImage.image = UIImage(named: "ic_question_bej")

I want to count sum of all elements that I add and finally set this counted value to my root view height. The problem is that

typeImage.frame.size.height
typeImage.bounds.size.height

always return 0.0

So how can I get height in CGFloat of newly added UIView via code? Maybe I should set height of rootview to wrap all my subview in another way?

Edit

Xcode loads view with some delay and if you will try to get view size immediately after adding it to parent you will get 0.0 sizes.

You have to call yourCustomView.layoutIfNeeded() before cheking sizes.

Thanks Chenjtc for right Answer.

Bios90
  • 801
  • 2
  • 14
  • 26
  • Unrelated but it's easier to create the image view as `let typeImage = UIImageView(image: UIImage(named: "ic_question_bej"))`. – rmaddy Jun 30 '18 at 21:13
  • First thing that comes to mind is you are checking frame sizes too soon - try `viewDidLayoutSubviews`. Maybe - maybe `viewWillLayoutSubviews` but **certainly** not `viewDidLoad`! (If you are doing this, I'd suggest checking this out: https://stackoverflow.com/questions/5562938/looking-to-understand-the-ios-uiviewcontroller-lifecycle ) One more thing that may help - since it seems you already know the height of each `typeImage` - if to "track" how many you've created. Arrays have a `count` property that is "1" based. Also,you could use a tag property that may help. –  Jun 30 '18 at 23:19

2 Answers2

1

If you want the sum of multiple views height in CGFloat you can do it like this :

let viewHeight = self.view.frame.height  //this in CGFloat
let sumHeight = viewHeight + ... other UIViews
print(sumHeight) // 568.0 for iPhone SE

or if you want to set your new UIView height you can do it by changing :

typeImage.heightAnchor.constraint(equalToConstant: 28).isActive = true

//

To :

typeImage.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 1).isActive = true
cs4alhaider
  • 1,318
  • 1
  • 17
  • 26
  • can i get height in float of a view that i add dynamically? For example i add 5 buttons with height of 25 CGFloat. And finally i want there rootView height to be button.height * 5. The problem is that button.frame.size always 0.0 – Bios90 Jun 30 '18 at 20:39
  • You can change (multiplier = 0.75) to take %75 of the view – cs4alhaider Jun 30 '18 at 20:43
  • Check my updated answer up .. if you want the height in CGFloat like this : 'view.frame.height' – cs4alhaider Jun 30 '18 at 20:45
  • Please mark my answer as the right one as it satisfy your posted question . – cs4alhaider Jun 30 '18 at 20:51
  • yes view.frame.height returns me height of my full screen. When i do myCustomRightNowAddedView.frame.height it returns 0.0 . Why it returns 0 , when i set it height to 28? – Bios90 Jun 30 '18 at 20:52
1

you can add the code like this: typeImage.layoutIfNeeded() before the code :
typeImage.frame.size.height typeImage.bounds.size.height constraint will not layout immediately, you should use layoutIfNeeded to layout the view, then you can get th frame

Chenjtc
  • 20
  • 3
  • after 6 hours of searching you finally answered my question!!! Yes after calling layoutIfNeeded() all works perfectly! – Bios90 Jul 01 '18 at 01:04