在 UISegmentedControl 中制作 2 个大小为 1 的段 [英] Make 2 segments the size of 1 in UISegmentedControl
问题描述
我有一个 UISegmentedControl
,其中在 storyboard
中创建了 3 个片段.我想以编程方式添加另一个段,并使中间的 2 个段与 1 个段的大小相同.这是一张图片来说明:
I have a UISegmentedControl
with 3 segments created in the storyboard
. I want to programmatically add another segment, and make the middle 2 segments the size of 1 segment. Here is an image to illustrate:
在storyboard
中:
我想以编程方式实现的目标:
What I want to achieve programmatically:
这是我试过的:
CGFloat segmentWidth = self.segment.frame.size.width;
[self.segment insertSegmentWithTitle:@"titleName" atIndex:2 animated:NO];
[self.segment setWidth:segmentWidth / 4 forSegmentAtIndex:1];
[self.segment setWidth:segmentWidth / 4 forSegmentAtIndex:2];
[self.segment setWidth:segmentWidth / 3 forSegmentAtIndex:0];
[self.segment setWidth:segmentWidth / 3 forSegmentAtIndex:3];
它没有给我想要的效果.宽度很奇怪.我怎样才能让宽度像上面的第二张图片一样?
It didn't give me the desirable effects. The widths were weird. How can I get the widths to be like the second image above?
推荐答案
1/3 的一半不是 1/4.是 1/6.
Half of 1/3 is not 1/4. It is 1/6.
您可能希望设置三个段,而让最后一个自动设置以填充其余部分.
You will probably want to set three of the segments and leave the last one to be set automatically to fill the remainder.
不要过早运行您的代码 - 如果您这样做,分段控件可能还没有达到其最终帧,因此您的 segmentWidth
将是错误的.
Don't run your code too early - if you do, the segmented control may not have achieved its final frame yet, so your segmentWidth
will be wrong.
这篇关于在 UISegmentedControl 中制作 2 个大小为 1 的段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!