Reputation: 7447
I've noticed that in IOS X-Code using (Swift 4.0), I can ask for the height of a view, V, in at least these two ways:
V.bounds.size.height
and...
V.bounds.height
Is there any actual difference between these two?
I did the option-click thing (which give different definitions, but don't explain any practical difference or reason for one over the other)... and stackoverflow... but here on stackoverflow, all the results are discussing the difference between bounds and frame... which is NOT what I'm asking.
Upvotes: 2
Views: 1637
Reputation: 131
V.bounds.height
is only a GET Property. You Can't set a value for this property.
Example:
self.view.bounds.height = 5
This error message results...
Cannot assign to property: 'height' is a get-only property
If you want to assign a value to this property, then you can write...
self.view.bounds.size.height = 5
So you can set value to this object. Have a look at here.
Upvotes: 6
Reputation: 1479
In addition to the fact that view.bounds.height
is readonly, there is another difference: if you have negative width/height, view.bounds.height
will return you the normalized value (the positive one), while view.bounds.size.height
will return the real value. These getters are the equivalent of the CGRectGetWidth()
CGRectGetHeight()
from Obj-C. All these getters from CGRect
struct (widht
, height
, minX
, minY
...) are returning the normalized values of the CGRect
's dimensions and they are recommended in case you want to use them in frame computations.
Upvotes: 2
Reputation: 9503
Actually V.bounds.size.height
, height have both get-set property and where as in V.bounds.height
, height is only getter property and it always return you height of the rectangle.
For the getter perspective both are same.
Upvotes: 1
Reputation: 5588
There is small difference. view.bounds.height
is a shortcut. You cannot edit it :
view.bounds.height = 150
won't work, but view.bounds.size.height = 150
will.
Upvotes: 1