UISegmentedControl 选定的段颜色
有没有办法在UISegmentedControl
中自定义所选段的颜色?
我找到了 segmentedController.tintColor
属性,它可以让我自定义整个分段控件的颜色。 问题是,当我为 tintColor
属性选择明亮的颜色时,选定的段几乎无法识别(其颜色几乎与分段控件的其余部分相同,因此很难区分选定和未选定的段)。所以我无法使用任何好的亮色来进行分段控制。 解决方案是选定段颜色的一些单独的属性,但我找不到它。有人解决这个问题了吗?
Is there any way to customize color of selected segment in UISegmentedControl
?
I've found segmentedController.tintColor
property, which lets me customize color of the whole segmented control.
The problem is, when I select bright color for tintColor
property, selected segment becomes almost unrecognizable (its color is almost the same as the rest of segmented control, so its hard to distinguish selected and unselected segments). So I cannot use any good bright colors for segmented control.
The solution would be some separate property for selected segment color but I cannot find it. Did anyone solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
我发现我可以在子视图上使用与段具有相同索引的标签,这样无论顺序如何,段都会正确着色。
I found I could use tag on the subviews with the same index as the segments, so that in any order they the segments will be colored correctly.
在段之间切换时,前两个解决方案对我不起作用。
我的解决方案是在视图控制器中处理段更改事件,然后在每次更改段时调用此方法:
The top two solutions didn't work for me when switching between segments.
My solution was to handle the segment change event in my view controller and then call this method each time the segment is changed:
我想知道为什么有人没有提到
UIAppearanceProxy
Apple Doc::
示例代码:
呼叫来源:
您可以在
AppDelegate
中调用此方法I am wondering why anyone have not mentioned about
UIAppearanceProxy
Apple Doc::
Sample Code:
Call From:
You can call this method in
AppDelegate
from为了做你这样的事情,人们可能必须访问未记录的功能和黑客行为,这肯定会让苹果愤怒,并可能导致你的申请被拒绝。
现在,解决方案在于其他技巧,即您使用两个按钮,并在单击它们时交换它们的图像。保持按钮靠近和半分段控制的图像,以给出分段控制的错觉,这就是我可以建议您的全部内容。
希望这有帮助。
谢谢,
马杜普
For doing your kind of thing, one might have to access the undocumented features and hacks, which will certainly make apple furious, and that may lead to the rejection of your application.
Now, the solution lies in other trick that you use two buttons instead and have their images interchanged when they are clicked. Keep the buttons closer and images of half segmented control to give the illusion of segmented control and that is all I can suggest you.
Hope this helps.
Thanks,
Madhup
您可以标记每个片段,然后设置 Tag 的 TintColor:
You can tag each of the segments, then set the TintColor forTag:
我发现上面的答案非常有帮助。我正在使用分段控件来设置旋钮的精度。我综合了上面的答案并得出了这个:
希望这对其他人有帮助..
对我来说,关键是能够使用以下命令重置未选择的索引:
setTintColor:nil];
I found the answers above very helpful. I am using the segmented control to set the precision of a knob. I took a hybrid of the answers above and came up with this:
Hope this helps others..
A key for me, was being able to reset the unselected index using:
setTintColor:nil];
这个 Swift 4 代码对我有用
This Swift 4 code works for me
这是将选定的段更改为任何 RGB 颜色的绝对最简单的方法。无需子类化或黑客攻击。
此示例显示了重要步骤:
“StyleBar”,这是必需的
设置
整个控件首先为橙色
线段变为绿色
注:
界面生成器,或代码为
显示。然而步骤3只能完成
在代码中
像这样的符号“123.0/255.0”是
只是一种生成 RGB 值的方法
脱颖而出,而不是标准化
UIColor 所需的浮点值
(如果你愿意就忽略它)
Here is the absolute simplest way to change the selected segment to any RGB color. No subclassing or hacks required.
This example shows the important steps:
"StyleBar", which is required for it
to work
entire control first to orange
segment to green
Notes:
interface builder, or in code as
shown. However step 3 can only be done
in code
notation like this "123.0/255.0" is
just a way to make the RGB values
stand out instead the normalized
float values required by UIColor
(just ignore it if you like)
我发现在 UISegmentcontrol 发送者中为选定的段添加颜色的简单方法
是 UISegmentControl
检查它对我有用
I found A Simple Way to Add Color for Selected Segment in UISegmentcontrol
sender is UISegmentControl
Check its Working For Me
为此,您只需找到选定的段,例如通过迭代分段控件的子视图并测试
isSelected
属性,然后只需调用该段的setTintColor:
方法即可子视图。我通过将一个操作连接到 Interface Builder 中 ValueChanged 事件上的每个分段控件来完成此操作,我将它们连接到视图控制器文件中的此方法,这本质上是 msprague 的答案:
为了确保每次用户打开视图时,控件都会正确显示,我还必须重写
-(void)viewDidAppear:animated
方法并按如下方式调用该方法:如果您确实想要的话,可以获得一些奖励积分将分段控件设置为在选择时使用白色色调,那么您还需要在选择文本时将其颜色更改为黑色,您可以这样做:
随着 iOS 6 的引入第一次在 viewDidAppear 方法中设置所选项目的色调颜色不起作用,为了解决这个问题,我使用中央调度在几分之一秒后更改所选颜色,如下所示:
To do this you simply have to find the selected segment, for example by iterating over the segmented control's subviews and testing the
isSelected
property, then simply call thesetTintColor:
method on that subview.I did this by connecting an action to each segmented control on the ValueChanged event in Interface Builder, I connected them to this this method in the view controller file which is essentially msprague's answer:
To ensure that the control is displayed correctly each time the view is opened by the user I also had to override the
-(void)viewDidAppear:animated
method and call the method as follows:For some bonus points if you do want to set the segmented control to use a white tint color on selection then you will also want to change the color of the text to black when it's selected, you can do this like so:
With the introduction of iOS 6 setting the tint color of the selected item for the first time in the viewDidAppear method wont work, to get around this I used grand central dispatch to change the selected color after a fraction of a second like so:
由于某种原因,Apple 不允许您更改标准 UISegmentedControls 的颜色。
然而,有一种“合法”的方法可以解决这个问题,即将分段控件样式更改为 UISegmentedControlStyleBar。这使它看起来略有不同,您可能不喜欢,但它确实允许颜色。
//更改栏样式和广告以查看然后释放分段控制器
希望这有帮助,
Seb Kade
“我是来帮忙的”
For some reason Apple dont allow you to change the color of standard UISegmentedControls.
There is however a "legal" way around it which is to change the segmented control style to UISegmentedControlStyleBar. This makes it look slightly different which you may not like but it does allow color.
//Change Bar Style and ad to view then release segmented controller
Hope this helped,
Seb Kade
"I'm here to help"
编辑:此解决方案不适用于 iOS 6。请参阅下面 David Thompson 的回答。
该线程确实很旧,但没有一个简单的答案对我来说合适。
只要您恢复取消选择的分段控件的颜色,接受的答案就有效。
像这样的东西将在您的值更改函数中起作用:
Edit: This solution doesn't work on iOS 6. See David Thompson's answer below.
This thread is really old, but none of the simple answers worked properly for me.
The accepted answer works as long as you revert the color of the deselected segmented controls.
Something like this will work in your value changed function:
我知道这是一个老问题但现在在 xcode 11 + 中,您可以设置选定的段色调颜色
在代码中我们可以使用
selectedSegmentTintColor
。适用于 iOS 13+I know this is an old question But now in xcode 11 +, you can set selected segment Tint colour
In code us can use
selectedSegmentTintColor
. available iOS 13+这是我对 uihacker 的 CustomSegmentedControl 的修改版本(请参阅评论中的信用)。我的想法是我改变了查找应该更改tintColor的子视图的方法,从使用selectedIndex到isSelected方法。因为我正在使用一个自定义 UISegmentedControl,它有 3 个或更多段,子视图顺序随机变化(即使 uihacker 的“hasSetSelectedIndexOnce”标志也不能解决这个问题!)。该代码仍处于早期开发阶段,因此使用它需要您自担风险。欢迎任何评论:)
另外,我添加了对界面生成器的支持,并覆盖 setSelectedSegmentIndex,以便它也更新颜色。享受!
CustomSegmentedControl.h
CustomSegmentedControl.m
Here is my modified version of uihacker's CustomSegmentedControl (see credit in comment). The idea is I change the way to find the subview that should have the tintColor changed, from using selectedIndex to isSelected method. Because I was working with a custom UISegmentedControl that has 3 or more segments which the subview ordering changes randomly (even uihacker's "hasSetSelectedIndexOnce" flag doesn't fix this!). The code is still in early dev stage so use it at your own risk. Any comment is welcomed :)
Also, I added support to interface builder, and override setSelectedSegmentIndex so that it also updates the color. Enjoy!
CustomSegmentedControl.h
CustomSegmentedControl.m
使用这个:
Use this:
不确定这是否会得到应用商店的批准,但我编写了一个 UISegmentedControl 的子类,它允许您设置自定义的选定和未选定颜色。查看注释以获取更多信息:
http://uihacker.blogspot .com/2010/05/iphone-uisegmentedcontrol-custom-colors.html
Not sure if this will get approved by the app store, but I wrote a subclass to UISegmentedControl that lets you set a custom selected and unselected color. Check the notes for more info:
http://uihacker.blogspot.com/2010/05/iphone-uisegmentedcontrol-custom-colors.html
为了澄清 @jothikenpachi 上面提供的答案,我们发现以下 UISegmentController 类别在 iOS6 中运行良好,并且允许分段上的任意开/关颜色方案。另外,如果私有方法 isSelected/setTintColor: 在未来的操作系统版本中发生更改,它将正常失败。关于私有 API 调用的注意事项等。
请注意,此类别方法将从 UISegmentController 的
- (IBAction) segmentAction: (id)sender
方法中调用。另请注意,对于 iOS6,您似乎可能需要首先在管理 UIViewController 的
- (void)viewDidAppear:(BOOL)animated
中调用此方法,这可能会导致动画闪烁。为了最大限度地减少这种情况,请尝试在 IB 中将“offColor”设置为 UISegmentController 的tintColor。To clarify the answer provided above by @jothikenpachi we found the following UISegmentController category worked well in iOS6 and allows for an arbitrary on/off color scheme on segments. Plus it will fail gracefully if the private methods isSelected/setTintColor: are changed in future OS releases. Caveats around private API calls, etc.
Note, this category method would be called from within the UISegmentController's
- (IBAction) segmentAction: (id)sender
method.Also note that with iOS6 it seems you may need to call this method initially in the governing UIViewController's
- (void)viewDidAppear:(BOOL)animated
which may result in a animation flash. To minimize this, try setting the "offColor" as the UISegmentController's tintColor in IB.我刚刚在 iOS 7 上遇到了这个问题,它的工作方式与 iOS6 不同。
在 iOS 7 中,所选段的标签颜色与 UISegementControl 背景颜色相同。在 iOS 7 上更改它的唯一方法是设置 UISegmentControl 的背景颜色。
I just ran into this issue on iOS 7, which works differently than iOS6.
In iOS 7, the color of the label for the selected segment is the same color as the UISegementControl background. The only way to change it on iOS 7 is to set the background color of the UISegmentControl.
我用了这个,它一步改变了所有颜色。
I used this and it changed all the colors in one step.