objective-c UISegmentedControl - 在界面生成器中改变高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2140492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:49:54  来源:igfitidea点击:

UISegmentedControl - altering height in Interface Builder

iphoneobjective-cinterface-builderheightuisegmentedcontrol

提问by Urizen

I'm creating a number of static custom UITableViewCellsand have dragged a UISegmentedControlonto one of the custom cells.

我正在创建一些静态自定义UITableViewCells并将 aUISegmentedControl拖到其中一个自定义单元格上。

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

虽然分段控件允许我改变它的宽度,但我不能在 Interface Builder 中改变它的高度(该属性在属性检查器的“大小”部分中的 44 上显示为灰色)。

I know that a UISegmentedControlcan 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?

我知道 aUISegmentedControl可以在代码中使用自定义高度创建并添加到 UITableViewCell。有什么办法可以在Interface Builder中调整分段控件的高度吗?

采纳答案by David Kanarek

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

不,它必须在代码中完成。看到这个问题。

回答by kender

You can also open the xibfile 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:

您还可以xib在任何文本编辑器中打开该文件,例如 Dashcode、MacVim 或 TextEdit——它是一个 XML。然后在那里找到你的元素,在我的例子中它看起来像:

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

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

然后您可以将其更改44为您想要的任何高度 - 您将看到 IB 的变化。

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

这适用于所有不能在 IB 中改变高度的元素 -UIPicker也......

回答by fschaper

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

当您在 Interface Builder 中固定“高度”时,您可以间接更改它(选择段控件,然后通过菜单选择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.

这将向该控件的约束列表添加一个新的高度约束,然后您可以对其进行编辑。更改直接反映在 InterfaceBuilder 中,如下面的屏幕截图所示。

interface builder height constraint

界面构建器高度约束

回答by Shaz

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

要在 Interface Builder 中执行此操作,您可以选择控件并在“用户定义的运行时属性”下添加框架属性

add frame attribute inside interface builder

在界面构建器中添加框架属性

回答by Neal L

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

我不确定在 IB 中,但您总是可以在代码中找到单元格并以编程方式调整其 UISegmentedControl 的高度。

回答by user4951

I want to add to Kender's answer.

我想补充 Kender 的答案。

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

如果您使用故事板而不是 XIB,则需要添加:

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

我猜这个 id 可以是任何独特的东西。也许选择一些ID并修改一个字母?

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.

我添加了一个 userLabel 以便我可以轻松找到这些东西。

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

注意:阅读故事板很有启发性。更改故事板文件非常危险。

回答by shreeji

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

我们可以设置自动布局,然后设置高度,然后我们可以关闭自动布局。所以它会帮助其他然后 ios 6.0 。