I have a custom UISegmentedControl. In iOS 6 and bellow it works fine. Under iOS 7.. it looks fine until I press the control, at which time, the divider image looks weird for a split second.
Here is my code:
UIImage *segmentSelected = [[UIImage imageNamed:@"segcontrol_sel.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(6, 6, 6, 6)];
UIImage *segmentUnselected = [[UIImage imageNamed:@"segcontrol_unsel.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(6, 6, 6, 6)];
UIImage *segmentSelectedUnselected =
[UIImage imageNamed:@"segcontrol_sel_uns.png"];
UIImage *segUnselectedSelected =
[UIImage imageNamed:@"segcontrol_uns_sel.png"];
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segUnselectedSelected
forLeftSegmentState:UIControlStateNormal // | UIControlStateHighlighted
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segUnselectedSelected
forLeftSegmentState:UIControlStateHighlighted
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal //| UIControlStateHighlighted)
barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];
UIFont *font = [UIFont systemFontOfSize:16.0f];
UIColor *textColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
font, @"NSFontAttributeName",
textColor, @"NSForegroundColorAttributeName",
nil];
[[UISegmentedControl appearance] setTitleTextAttributes:attributes
forState:UIControlStateNormal];
Any ideas what is happening when I press the UISegmentedControl that might cause the divider to be displayed wrong? Thanks?
I solved this in a way similar to what user2128193 describes, but instead of adding a target for the value changed event, I subclassed UISegmentedControl and added these two methods:
- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents
{
[super sendActionsForControlEvents:controlEvents];
if (controlEvents & UIControlEventValueChanged) {
[self removeAnimationsRecursivelyForView:self];
}
}
- (void)removeAnimationsRecursivelyForView:(UIView *)view
{
[view.layer removeAllAnimations];
for (UIView *subview in [view subviews]) {
[self removeAnimationsRecursivelyForView:subview];
}
}
Obviously this is still not a perfect solution, in that it relies on the internals of UISegmentedControl, but at least it will keep your code a bit cleaner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With