我正在改变所选片段的颜色。它工作正常但是uisegment控制色调颜色被设置为默认值,直到它第一次被触摸。
这是更改所选分段颜色的方法。它工作正常,但当细分市场第一次出现时。它有浅灰色。然后当它被触摸时,它会在我需要时开始正常工作。
(此段控件作为子视图添加到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];
}
}
}
答案 0 :(得分:4)
您可以在 viewDidLoad 方法中强制调用segmentControlValueChanged
方法。像这样:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self segmentedControlValueChanged:segmentedControlOne];
});
答案 1 :(得分:1)
您可以从viewdidload或viewwillappear方法中更改它吗?因为根据您的代码,当用户第一次触摸它时它会发生变化。
答案 2 :(得分:1)
打开这段代码
我在这段代码中解决了我的应用
- (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];
}
}
});
}
}
}
答案 3 :(得分:0)
我自己想通了。由于alertview仅在[alertview show]之后创建所有视图。由于尚未真正创建segmentcontrol,因此不会检测到segmentcontrol的颜色更改。所以我做的是我推迟了段控制色彩变化的过程,直到它被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];
});