Reputation: 4884
The arranged subviews in UIStackView
with a horizontal axis must have the same height with the UIStackView
's height. However it is not true for a UISlider
. Whatever height I set for UIStackView
, the height of UISlider
in the stack view will have 1px taller height.
I set UIStackView
's height as 40 and didn't set any auto layout for the UISlider
.
But the height of UISlider
is 41 which I expected 40.
Is there any reason for this? and any way to fix this issue?
Upvotes: 0
Views: 241
Reputation: 77462
UISlider
seems to do some odd stuff with its sizing.
Ignoring the stack view for the moment, if you simply apply width and height constraints to a UISlider
, they won't match the actual width and height at run-time. You get an extra 4-pts
width and an extra 1-pt
height.
However...
If you constrain another view relative to the slider, the other view respects the slider's constraints, not the slider's rendered size.
Take a look at this image-capture from Debug View Hierarchy:
width = 100
height = 40
but the orange rect (the slider background) is actually 104 x 41
- and that's what you'll get if you print(mySlider.frame)
at run-time.
Left Label has
trailing = slider.leading
height = slider.height
centerY = slider.centerY
Right Label has
leading = slider.trailing
height = slider.height
centerY = slider.centerY
Both label heights are exactly 40-pts - which matches the constraint.
Note also that the label frames overlap the slider background by 2-pts
, showing that the label x-positioning is, again, matching the constraints, not the rendered slider view frame.
If you inspect the views when the slider is embedded in a UIStackView
, you will see (well, you have seen) that the frame size is greater than the stack view's frame. But, the slider's frame will extend outside the actual frame of the stack view... the stack view (and its other arranged subviews) will continue to respect the constraints you've given it.
All of which is to say... if the "size mismatch" is not causing any issues, I wouldn't worry about it.
Upvotes: 2