UISegmentedControl - altering height in Interface

2019-03-11 00:25发布

问题:

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?

回答1:

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



回答2:

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...



回答3:

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.



回答4:

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



回答5:

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



回答6:

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.



回答7:

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