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.