:: iDavidStudio ::..

Dev log

iOS 5 Weird Behavior with the UISegmentedControl setDividerImage

without comments

 

I’m using the appearance feature in the iOS 5 to customise the UISegmentedControl. Initially I was using the standard way to define different images for different divider status, it working fine when I tapped on each item. But when I set up the initial selected value to anything that rather than 0 (Zero), the divider behavior very weird:

 

 

 

* Images for different status:

Normal – Normal
Selected – Normal
Normal – Selected

 

 

 

// — Images for divider at different status.

UIImage *imgSelectedUnSelected = [[UIImage imageNamed:@”segments-selected-unselected.png”] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *imgUnSelectedUnSelected = [[UIImage imageNamed:@”segments-unselected-unselected.png”] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *imgUnSelectedSelected = [[UIImage imageNamed:@”segments-unselected-selected.png”] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

// — Dividers
[[UISegmentedControl appearance] setDividerImage:imgSelectedUnSelected forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:imgUnSelectedUnSelected forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:imgUnSelectedSelected forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

 

 

The way to fix this issue, I use a 1 pixel width image for the divider instead of the 3 different images above.

1 pixel divider imag

 

// — Dividers 1pixel images

UIImage *imgSelectedUnSelected = [[UIImage imageNamed:@”segment-divider.png”] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *imgUnSelectedUnSelected = [[UIImage imageNamed:@”segment-divider.png”] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *imgUnSelectedSelected = [[UIImage imageNamed:@”segment-divider.png”] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

 

It works fine now:

 

 

Written by admin

August 12th, 2012 at 6:11 pm

Leave a Reply

You must be logged in to post a comment.