具有动态大小和 SegmentedItems 的 UISegmentedControl

发布于 2024-11-08 22:20:17 字数 1446 浏览 0 评论 0原文

我在代码中创建一个 UISegmentedControl 并使用从 JSON 响应检索到的名称对其进行初始化。 btnArray 保存要在segmentedItems 上显示的名称。第一项是固定的“全部”按钮。

我的问题是源数组包含 4-8 个数字,因此这会导致段上的文本显示在 segmentItem 之外的问题。

有没有办法让段具有可变大小,具体取决于段项的数量,以便它仍然适合工具栏内?

另外,如何控制显示文本的长度,即如果文本不适合内部,则剪裁文本,类似于 UIButton 的相同行为。

或者有人对使用“下一个”和“上一个”导航解决方案有任何提示,可以在其中切换新的分段吗?这样我就可以有一个固定的 nr 来始终显示前 4 个,并且可以在控件上滑动新的段,也可以导航回前 4 个。

这是我的代码:

segmentedControl = [[UISegmentedControl alloc] initWithItems:btnArray];
segmentedControl.tintColor = [UIColor grayColor];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 300, 30);

// Setup the target and actions for the segmentedControl
[segmentedControl addTarget:self 
                     action:@selector(selectGroup:) 
           forControlEvents:UIControlEventValueChanged];

// Add the UISegmentedControl as a UIBarButtonItem subview to the UIToolbar
UIBarButtonItem *segmentedItem = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *groupsButtons = [NSArray arrayWithObjects:flexSpace, segmentedItem, flexSpace, nil];

[groupsToolbar setItems:groupsButtons];

I create a UISegmentedControl in code and initialize it with names retrieved from a JSON response. The btnArray holds the names to display on the segmentedItems. The first item is a fixed "All" button.

My problem is that the source array contains between 4-8, so this leads to problem that the text on the segment is displayed outside the segementItem.

Is there any way to let the segment have a variable size depending on how many segmentItems so it still fits inside the toolbar?

Also, how can I control the length of the text displayed, that is clip the text if it does not fit inside, similar to the same behavior for a UIButton.

Or does anyone have any tips for having a navigation solution with "next" and "previous", where its possible to toggle new segments? In that way I could have a fixed nr to always display first 4 and have the possibility of sliding in new segments on the control and also navigating back to previous 4.

Here is my code:

segmentedControl = [[UISegmentedControl alloc] initWithItems:btnArray];
segmentedControl.tintColor = [UIColor grayColor];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 300, 30);

// Setup the target and actions for the segmentedControl
[segmentedControl addTarget:self 
                     action:@selector(selectGroup:) 
           forControlEvents:UIControlEventValueChanged];

// Add the UISegmentedControl as a UIBarButtonItem subview to the UIToolbar
UIBarButtonItem *segmentedItem = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *groupsButtons = [NSArray arrayWithObjects:flexSpace, segmentedItem, flexSpace, nil];

[groupsToolbar setItems:groupsButtons];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你穿错了嫁妆 2024-11-15 22:20:17

听起来像 UITabBar 是什么你正在寻找。如果项目超过 4 个,您将免费获得一个“更多...”按钮,其中显示其余项目。

Sounds like a UITabBar is what you're looking for. If there are more than 4 items, you get a "More..." button for free that shows the rest of the items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文