ios - How to specify a constraint for a UILabel to remain centered beneath each segment of a UISegmentedControl? -


in app ios 8, have uisegmentedcontrol stretches fit width of device's screen. on ipad it's more pixels wide on iphone 6+, more pixels wide iphone 6, etc.

centered beneath each segment of uisegmentedcontrol, have uilabel. there 5 segments , 5 uilabels. each uilabel has fixed width (fixed constraint). if display size increases become uncentered.

how in interface builder can specify constraint force each uilabel become centered beneath each segment? happy if elements remain proportionally spaced each other display size scales, can't figure out how that, either.

all can seemingly center middle uilabel directly under middle segment specifying center x alignment between , uisegmentedcontrol.

i specified horizontal space constraint between uilabels, , between outer uilabels , edges of view, , set these "greater or equals". have same priority, strangely, don't scale proportionally each other.

the resulting problem amount of horizontal space between each of uilabels not scale smoothly width of device's screen increases. if align in proper positions on iphone 5s width of screen, on ipad alignment wonky, , middle 1 lines segment. rest of them off center.

it appears there no way specify percentage of over-all display width constraint -- can specify things in terms of pixels. really?!?!

clearly make width of objects flexible, because text labels right-aligned text, screws up.

surely i'm missing here... since point of auto layout make interface scale according screen size, surely there way specify constraint as percentage of given view or subview... surely!!! how? i've read documentation , cannot, life of me, figure out.

btw did see in past, people have used crude hacks spacer views or multiple sets of constraints, surely outdated answers, , i'm overlooking extraordinarily obvious... right?

you can making centerx constraint of labels equal superview.trailing times 0.1, 0.3, 0.5, 0.7, , 0.9 constants of 0. make these constraints, add 5 labels view. give left 1 vertical spacing constraint segmented control. select 5 labels , give give them "vertical centers" alignment constraint. control-drag each label right side of screen, , select "trailing space container margin" constraint. edit each 1 of these trailing constraints (except multiplier needs given values mentioned above):

enter image description here

you'll have reverse first , second item (which pull down on first item), change label.trailing label.center x, , uncheck "relative margin" box, correct constant , multiplier values.

this approach work if segmented control stretches way across screen no padding edges. if want padding edges, need use different approach. need create 5 uiviews below segmented control -- align left edge of left-most 1 left edge of segmented control. align right edge of right-most 1 right edge of segmented control. give 5 views equal width, , 0 length horizontal spacing constraint each neighbor. give 5 views mimic segmented control in width, each view being same width 1 of segments (assuming segments same width -- if that's not case, you're screwed). need add labels subviews of these 5 views, , give them centerx , centery constraints.


Comments

Popular posts from this blog

Java 8 + Maven Javadoc plugin: Error fetching URL -

css - SVG using textPath a symbol not rendering in Firefox -

c - gcc compile error: unknown type name 'File' -