20

I'm creating a number of static custom UITableViewCells and have dragged a UISegmentedControl onto one of the custom cells.

Whilst the segmented control allows me to alter its width I cannot alter its height in Interface Builder (that property is greyed out on 44 in the 'size' section of the property inspector).

I know that a UISegmentedControl can be crated with a custom height in code and added to a UITableViewCell. Is there any way to adjust the height of the segmented control in Interface Builder?

Urizen
  • 2,331
  • 6
  • 23
  • 33

7 Answers7

39

You can also open the xib file in any text editor, like Dashcode or MacVim or TextEdit - it's an XML. Then find your element there, in my case it looked like:

<object class="IBUISegmentedControl" id="270020637">
    [...]
    <string key="NSFrame">{{20, 154}, {176, 44}}</string>
    [...]
</object>

Then you can change the 44 into whatever height you want - you'll see the changes in IB.

This works for all elements that can't be changed height in IB - UIPicker also...

kender
  • 85,663
  • 26
  • 103
  • 145
35

You can indirectly change it when you pin the "Height" in Interface Builder (select the segment control then via the menu select Editor -> Pin-> Height).

This will add a new Height constraint to the list of constraints for that control which you can then edit. Changes directly reflect in InterfaceBuilder as you can see in the Screenshot below.

interface builder height constraint

fschaper
  • 721
  • 7
  • 10
  • 4
    Great help. Thanks. Yes iOS6 only, but as of Feb 2013, that was 83% of iOS devices. http://www.tuaw.com/2013/02/13/chitika-all-versions-of-ios-6-now-account-for-83-1-of-ios-traf/ – B-Money May 21 '13 at 22:56
  • When it's embeded in navigation bar, seems we can't change. – ZYiOS Mar 21 '14 at 15:11
10

No, it must be done in code. See this question.

Community
  • 1
  • 1
David Kanarek
  • 12,611
  • 5
  • 45
  • 62
2

I'm not sure in IB, but you could always find the cell in your code and programmatically adjust its UISegmentedControl's height.

Neal L
  • 4,329
  • 8
  • 34
  • 39
2

To do it inside Interface Builder you can select the control and add frame attribute under "User Defined Runtime Attributes"

add frame attribute inside interface builder

Shaz
  • 958
  • 7
  • 9
1

I want to add to Kender's answer.

If you use storyboard rather than XIB, you will need to add:

                                <constraint firstAttribute="height" constant="10" id="9Wo-6S-8EM"/>
                                <constraint firstAttribute="width" constant="201" id="lw7-cq-3XN"/>

The id can be anything unique I guess. Maybe pick some ID and modify one letter?

This is the full code

<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="RMR-XS-abw" userLabel="ngentot2">
                        <constraints>
                            <constraint firstAttribute="height" constant="10" id="9Wo-6S-8EM"/>
                            <constraint firstAttribute="width" constant="201" id="lw7-cq-3XN"/>
                        </constraints>
                        <segments>
                            <segment title="First"/>
                            <segment title="Second"/>
                        </segments>
                    </segmentedControl>

I add a userLabel so I can easily find the stuff.

Note: reading the storyboard is very enlightening. Changing storyboard file is VERY dangerous.

user4951
  • 32,206
  • 53
  • 172
  • 282
1

We can set autolayout and then set height, After that we can off autolayout. so It will help for other then ios 6.0 .

shreeji
  • 65
  • 7