Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The trick appears to be to size the <a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UISegmentedControl_Class/" rel="nofollow noreferrer">UISegmentedControl</a> to the size of the <code>backgroundView</code> of the control, not the <code>contentView</code>. I was able to do it programmatically by doing the following:</p> <pre><code> // Size to cover the entire background self.contentView.frame = self.backgroundView.frame; self.myControl.frame = self.contentView.bounds; </code></pre> <p>Note that if you are using an accessory, you need to account for the <code>accessoryView</code> as well.</p> <p>The reason is that the view hierarchy is as follows:</p> <ul> <li><code>self</code> (the <a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewCell_Class/" rel="nofollow noreferrer">UITableViewCell</a> or subclass) <ul> <li><code>backgroundView</code></li> <li><code>contentView</code> <ul> <li><em>(your controls go here)</em></li> </ul></li> <li><code>accessoryView</code></li> </ul></li> </ul> <p>In portrait layout, the <code>backgroundView</code>'s frame is <code>{{9, 0}, {302, 44}}</code>, whereas the <code>contentView</code>'s frame is slightly smaller, at <code>{{10, 1}, {300, 42}}</code>. This gives the cell its 1px "border" when the table style is grouped. You have to resize both the <code>contentView</code> <em>and</em> your control to get the appropriate size.</p> <p>(NOTE: While Apple actually has several examples of a <a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UISegmentedControl_Class/" rel="nofollow noreferrer">UISegmentedControl</a> in the <a href="https://developer.apple.com/iphone/library/samplecode/UICatalog/index.html" rel="nofollow noreferrer">UICatalog</a> sample code project in <a href="https://developer.apple.com/iphone/library/navigation/index.html" rel="nofollow noreferrer" title="iPhone OS Reference Library">the SDK</a>, they effectively "cheat" by using a <a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIViewController_Class/" rel="nofollow noreferrer">UIViewController</a> and setting the main view's background color to the table background color.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload