ios - UISegmentedControl 在 vi​​ewLoad 上选择了 tintColor

标签 ios objective-c ipad uisegmentedcontrol

我正在尝试为 UISegmentedControl 设置 tintColor 和选定的 tintColor。

到目前为止一切正常,除了当 View 首次加载时,即使我调用我的方法来设置 tintColor(并执行),tintColor 实际上直到第一次点击才正确设置。

不过它确实出现了,好像某事正在发生,因为我正在尝试将颜色设置为黑色并选择白色,而默认颜色有点暗。

关于如何实际修改负载颜色的任何想法?

//some code
-(void)viewDidLoad
_segmentedControl.selectedSegmentIndex = 0;
[self segmentedControl:_segmentedControl];

//set color method
- (IBAction)segmentedControl:(BaseSegmentedControl *)sender
{
//change the color of every subview(segment) you have
 for (int i = 0; i < [[sender subviews] count]; i++ )
{
    if ([[sender.subviews objectAtIndex:i]isSelected] )
    {
        [[sender.subviews objectAtIndex:i] setTintColor:nil];
        [[sender.subviews objectAtIndex:i] setTintColor:[UIColor whiteColor]];
    }
    else
    {
        [[sender.subviews objectAtIndex:i] setTintColor:nil];
        [[sender.subviews objectAtIndex:i] setTintColor:[UIColor blackColor]];
    }
}

谢谢

最佳答案

我认为您可能已将 uisegmentedcontrol 放到导航栏中。当您这样做时,UISegmentedControl 在您的 View 之前加载,因此设置色调颜色是没有用的。我在这里找到解决方案:

 self.segmentedControll.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;

正如文件所说:

When this property’s value changes (either by the view’s value changing or by one of its superview’s values changing), -the system calls the tintColorDidChange method to allow the view to refresh its rendering.

将这一行添加到您的 viewDidLoad 方法中,这将导致 segmentedcontroll 重绘。

关于ios - UISegmentedControl 在 vi​​ewLoad 上选择了 tintColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17821497/

相关文章:

ios - 委托(delegate)方法不能引用自己的类

ios - 从sqlite数据库获取数据-iOS

iPhone 应用程序崩溃并且没有留下 .crash 日志文件

ios - 使用后台线程在日历上创建多个事件

ios - 使用脏矩形优化 OpenGL ES 2.0 2D 绘图

iphone - 配置文件问题

iphone - iPad 和 iPhone 的不同布局

iphone - iOS从NSString中删除前N个单词

ios - 使用推送通知发送数据 - 也不返回整个推送

iphone - 语音到 iPhone 的文本 API?