need to resize a control (UISlider) to fill all available UIToolbar space (iOS)
我需要调整UISlider的大小以填充所有可用的UIToolbar空间,并且它必须在iOS 4.3和iOS 5.0下工作。 (嗯,我有一些适用于5.0的东西,但对于4.3来说仍然是一个难题。)
有一个UIToolbar,其按钮是动态配置的,包括仅用于横向显示的按钮。 (完整的设置在"界面生成器"中指定,其中一些通过
我希望滑块填充两组按钮(左组和右组)之间的所有可用空间。
这是我计算所有条形按钮项目的宽度的方式。
主要思想是它们的顺序并不重要,我们可以使用IBOutletCollection:
1 | @property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *allButtons; |
,以及在更改方向之前执行的代码中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | const CGFloat leftOrRightMargin = 12; // measured on a screenshot const CGFloat gapBetweenButtons = 10; // measured on a screenshot const CGFloat experimentalCorrection = 6; // don't ask why int totalNumberOfButtons = 0; CGFloat totalWidthOfButtons = 0.0; for (UIButton *b in allButtons) { if (b.superview) { // if it's visible totalNumberOfButtons++; totalWidthOfButtons += b.bounds.size.width; } } CGFloat occupiedSpace = experimentalCorrection + 2 * leftOrRightMargin // gaps: totalNumberOfButtons-1+1, the last one is for the slider + totalNumberOfButtons * gapBetweenButtons + totalWidthOfButtons; // now, resize the slider control |
方向更改后的
:
1 | [myBar setNeedsLayout]; |