- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这个问题相当简单,但我在许多 UISegmentedControl 帖子中没有看到任何真正满足我需求的内容:
环境:我有一组显示在 UISegmentedControl 中的目录。这是一个平面层次结构,目录很少,因此这是显示它们的最佳方式。
选择一个段会用该目录的内容填充下面的 UITableView。
我可以通过编程方式选择给定的片段,这样我就可以根据需要选择合适的片段。
效果很好。
问题:
其中一个目录是“默认”目录,它将包含现有项目和新项目的混合。
我想标记该片段,以便指示其中有多少新片段,以便人们知道选择它(如果尚未为他们选择的话)。
这意味着我需要访问 UISegmentedControl 中的实际 subview 和诸如此类的东西。
没那么简单。创建角标(Badge)是 children 的游戏。弄清楚角标(Badge)放在哪里是成年人的事。
看起来 Apple 有意隐藏对分割市场的直接访问。您只能影响整个控件。
关于我如何只修改一个片段,甚至找出该片段的位置,有没有人有任何建议?
widthForSegmentAtIndex: .功能似乎毫无值(value),因为它是否会给您任何有用的信息是任意的。
最佳答案
不,widthForSegmentAtIndex:
返回的值不是任意的。正如您在文档中看到的那样,它返回段的宽度或 0.0,这意味着段是自动调整大小的。
有一种方法可以获取每个片段的帧。
或者在代码中:
据我在 iOS7 上看到的,段之间的“边界”不是段宽度的一部分。
CGFloat autosizedWidth = CGRectGetWidth(self.segment.bounds);
// iOS7 only?!
autosizedWidth -= (self.segment.numberOfSegments - 1); // ignore the 1pt. borders between segments
NSInteger numberOfAutosizedSegmentes = 0;
NSMutableArray *segmentWidths = [NSMutableArray arrayWithCapacity:self.segment.numberOfSegments];
for (NSInteger i = 0; i < self.segment.numberOfSegments; i++) {
CGFloat width = [self.segment widthForSegmentAtIndex:i];
if (width == 0.0f) {
// auto sized
numberOfAutosizedSegmentes++;
[segmentWidths addObject:[NSNull null]];
}
else {
// manually sized
autosizedWidth -= width;
[segmentWidths addObject:@(width)];
}
}
CGFloat autoWidth = floorf(autosizedWidth/(float)numberOfAutosizedSegmentes);
for (NSInteger i = 0; i < [segmentWidths count]; i++) {
id width = segmentWidths[i];
if (width == [NSNull null]) {
[segmentWidths replaceObjectAtIndex:i withObject:@(autoWidth)];
}
}
CGFloat x = CGRectGetMinX(self.segment.frame);
for (NSInteger i = 0; i < [segmentWidths count]; i++) {
NSNumber *width = segmentWidths[i];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, CGRectGetMaxY(self.segment.frame) + 1, [width floatValue], 30)];
view.backgroundColor = [UIColor colorWithHue:i/(float)[segmentWidths count] saturation:1 brightness:1 alpha:1];
[self.view addSubview:view];
x = CGRectGetMaxX(view.frame)+1;
}
这会产生以下结果:
我建议您不要将角标(Badge)添加为 UISegmentedControl 的 subview ,您可以将其添加到 segmentedControl 的 superView。您的角标(Badge)基本上应该是 segmentedControl 的兄弟
请提交 enhancement request与苹果公司。他们不会让我们访问各个 subview ,但他们至少可以告诉我们段的实际大小。
关于iOS:如何访问 UISegmentedControl 中的各个段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19126728/
根据以下文档 ( https://developer.apple.com/documentation/uikit/uisegmentedcontrol/1618570-settitletextattr
我试图在登录时隐藏 nameTextField 并使其在注册时显示切换。我一直试图自己解决这个问题,但这就是我所得到的。我已经把它归结为两个错误,它们是相同的。 Issue Navigator laz
我有两个 UISegmentedControl,我想用其中一个来启用/禁用另一个。我编写了一个函数来处理这个问题,如下所示: - (void)disableSegment2 { if (
我有一个以编程方式定义的 UISegmentedControl。 我正在尝试添加布局约束,以便当我的 iPad 旋转时,分段控件在旋转 View 中正确调整大小,而不是溢出屏幕。 我应用了以下约束:
如何通过再次按下同一段来取消选择 UISegmented 控件中的给定段? 例如按下段 0,它将被选中并保持突出显示。再次按下段 0,它将变为未选中且未突出显示。 该控件仅触发 UIControlEv
我正在为我的分段控件设置颜色,如下所示: segmentedControl.backgroundColor = .gray segmentedControl.selectedSegmentTintCo
我在 iOS7 中更改分段控件的边框颜色时遇到问题。我在 stackoverflow 的其他地方找到了以下建议: [[UISegmentedControl appearance] setTit
是否可以让 UISegmentedControl 在 iPhone 上跨越两行?我已经在一些应用程序中看到了这一点,但没有在文档中找到我需要的内容。也许它是一个自定义的 UIButton,设计得像 U
我的应用程序中有一个 UISegmentedControl(请参阅下面的代码): // --------------- SETTING NAVIGATION BAR RIGHT BUTTONS NSA
我的 iPhone 应用程序中有一个分段控件,在 ios6 上运行良好,但在 ios7 上,分段图 block 被截断(有足够的空间容纳文本,但无论如何都会截断它们) self.segment
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How to change font color of UISegmentedControl 是否可以为 selec
有没有办法通过编程使UIsegmentedControl具有方角?请帮忙!!! 最佳答案 您可以为将完全覆盖边框的片段设置背景图像。如果背景图像是方形的,那么分段控件将显示为方形。对您想要自定义的每个
如何制作多行 UISegmentedControl。我需要它有 6 个按钮,每行 3 个。我如何以编程方式执行此操作? 最佳答案 您将需要使用其中两个,并使用 selectedSegmentIndex
我正在使用带有一些自定义图像的 UISegmentedControl: UISegmentedControl *segmentedControl = [[UISegmentedControl allo
有没有办法摆脱 UISegmentedControl 的圆角或者它是默认行为? 最佳答案 有一些非常简单的方法可以让您摆脱 UISegmentedControl 上的圆形垃圾...将样式更改为“7”。
就像标签栏一样,我想在 UISegmentedControl 上显示徽章。因为我看不到 UISegmentedControl 的任何预定义方法,就像可用于 UITabBar 的方法一样。 我考虑过将徽
如何获得一个类似于邮件应用程序中的 UISegmentedControl,以便它与 UIToolbar 按钮颜色相同(就好像两个段都处于选定状态一样)。 我想使用分段控件来实现与 Mail 完全相同的
这可能是一个非常简单的问题,但我似乎无法在 API 或任何搜索引擎中找到任何内容。 我有一个分段控件,我已将其设置为瞬时,因为用户将选择他们想要搜索的几个汽车品牌。我遇到的问题是我似乎无法弄清楚如何识
当我将整个导航栏设置为黑色时,通常在所选按钮上的 UISegmentedControl 上显示的色调不会显示(self.navigationController.navigationBar.tintC
我想为 UISegmentedControl 提供以下方面: 注意灰色背景 View ,以及分段控件未选定项目的白色背景。 但是,如果我为 UISegmentedControl 提供白色背景,我会得到
我是一名优秀的程序员,十分优秀!