The trick appears to be to size the UISegmentedControl to the size of the backgroundView
of the control, not the contentView
. I was able to do it programmatically by doing the following:
// Size to cover the entire background
self.contentView.frame = self.backgroundView.frame;
self.myControl.frame = self.contentView.bounds;
Note that if you are using an accessory, you need to account for the accessoryView
as well.
The reason is that the view hierarchy is as follows:
self
(the UITableViewCell or subclass)
backgroundView
contentView
accessoryView
In portrait layout, the backgroundView
's frame is {{9, 0}, {302, 44}}
, whereas the contentView
's frame is slightly smaller, at {{10, 1}, {300, 42}}
. This gives the cell its 1px "border" when the table style is grouped. You have to resize both the contentView
and your control to get the appropriate size.
(NOTE: While Apple actually has several examples of a UISegmentedControl in the UICatalog sample code project in the SDK, they effectively "cheat" by using a UIViewController and setting the main view's background color to the table background color.)