Make 2 segments the size of 1 in UISegmentedControl

↘锁芯ラ 提交于 2019-12-13 08:25:27

问题


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:

In storyboard:

What I want to achieve programmatically:

This is what I tried:

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:


  1. Half of 1/3 is not 1/4. It is 1/6.

  2. You will probably want to set three of the segments and leave the last one to be set automatically to fill the remainder.

  3. 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.



来源:https://stackoverflow.com/questions/30179886/make-2-segments-the-size-of-1-in-uisegmentedcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!