简体   繁体   English

细分控制色调颜色

[英]uisegment control tint color

Am changing the color of the selected segment. 正在更改所选片段的颜色。 It works fine but uisegment control tint color is set to default until it is touched for the first time. 它可以正常工作,但是将uisegment控件的颜色设置为默认,直到首次触摸。

This is the method that changes the color for the selected segment. 这是更改所选段的颜色的方法。 It works fine but when the segment shows up first time. 效果很好,但是该细分是首次出现。 It has pale gray color. 它具有浅灰色。 Then when touched it starts working fine as I needed. 然后,当我触摸它时,它就会开始正常工作。

(This segment control is added as subview to uialertview) (此细分控件作为子视图添加到uialertview)

 -(void)segmentValueChanged:(UISegmentedControl*)sender 
  {

        for (int i=0; i<[sender.subviews count]; i++)
        {
            if ([[sender.subviews objectAtIndex:i]isSelected] )
            {
                UIColor *tintcolor=[UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
                [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];

                trackType = sender.selectedSegmentIndex;
            }
            else{
                UIColor *tintcolor=[UIColor colorWithRed: 225/255.0 green:220/255.0 blue:210/255.0 alpha:1.0];
                [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
            }

        }

    }

you can call segmentControlValueChanged method forcefully in viewDidLoad method. 您可以在viewDidLoad方法中强制调用segmentControlValueChanged方法。 Like this: 像这样:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self segmentedControlValueChanged:segmentedControlOne];
    }); 

Can you please change it from viewdidload or viewwillappear method of your view? 您可以从视图的viewdidload或viewwillappear方法更改它吗? because as per your code, it will change, when user first time touch it. 因为根据您的代码,当用户第一次触摸它时,它会改变。

truy this code 验证此代码

i solved my app in this code 我用这段代码解决了我的应用程序

- (void)viewDidAppear:(BOOL)animated

    {

        for(UIView *v in [self.view subviews] )
        {
            if([v isKindOfClass:[UISegmentedControl class]])
            {
               // ((UISegmentedControl*)v).enabled=NO;

                dispatch_async(dispatch_get_main_queue(),^{

                    for (int i=0; i<[ ((UISegmentedControl*)v).subviews count]; i++)

                    {

                        if ([[((UISegmentedControl*)v).subviews objectAtIndex:i]isSelected] )

                        {

                            [[((UISegmentedControl*)v).subviews objectAtIndex:i] setTintColor:[UIColor colorWithRed: 0/255.0 green:176/255.0 blue:223/255.0 alpha:1.0]];
                            //[[((UISegmentedControl*)v).subviews objectAtIndex:0]setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                            //  break;

                        }
                        else
                        {
                            [[((UISegmentedControl*)v).subviews objectAtIndex:i] setTintColor:[UIColor whiteColor]];
                            //[[((UISegmentedControl*)v).subviews objectAtIndex:0]setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

                        }


                    }


                });

            }
        }

    }

I figured it out myself. 我自己弄清楚了。 Since alertview creates all the views only after [alertview show]. 由于alertview仅在[alertview show]之后创建所有视图。 color changes to segmentcontrol is not afftected since segmentcontrol is not really created yet. 由于尚未真正创建细分控件,因此尚未更改细分控件的颜色。 So what I did was I delayed the process of segmentcontrol tintcolor changes until it's been created by alertview 所以我所做的是我延迟了segmentcontrol tintcolor更改的过程,直到由alertview创建它为止

[alertview show];

double delayInSeconds = 0.01;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [self performSelectorOnMainThread:@selector(segmentValueChanged:) withObject:segControl waitUntilDone:YES];

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM