简体   繁体   English

如何设置UISegmentedControl框架?

[英]How to set UISegmentedControl frame?

Here is my code: 这是我的代码:

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
segmentedControl.frame = CGRectMake(0.0, 0.0, 320.0, 30.0);

The result is segmentedControl has about 10 pts margin left and top. 结果是segmentedControl左边和上边约有10个边距。

I try to change the params of its frame but cannot change x position. 我尝试改变其框架的参数但不能改变x位置。 So it always has 10 pt margin left. 所以它总是有10磅的余量。 How to remove this margin? 如何删除此保证金?

Edit 编辑

I figure it out 我明白了

UIToolbar has 10pt margin. UIToolbar有10pt的保证金。 If I add only UISegmentedControl, it will display correctly. 如果我只添加UISegmentedControl,它将正确显示。

you can't change the height of UISegmentedControl because height is fixed for UISegmentedControl based on its style 你不能改变的高度UISegmentedControl因为高度是固定的UISegmentedControl基于它的风格

for plain and borderd style the height is 43 and for bar style the height is 29. 对于普通和边框样式,高度为43,对于条形样式,高度为29。

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
segmentedControl.frame = CGRectMake(0.0, 0.0, 320.0, 29.0);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

set UISegmentedControlStyleBar in segmentedControlStyle property of UISegmentedControl . UISegmentedControlsegmentedControlStyle属性中设置UISegmentedControl

Use this 用这个

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];  
segmentedControl.frame = CGRectMake(35, 200, 250, 50);  
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;

Use setter method 使用setter方法

[segmentedControl setFrame:CGRectMake(x, y, width, height)];

Segmented control has the rounded rect border and that is why it displayse so.Jus set some color on its background and you can see the correct frame of the segment 分段控件具有圆角矩形边框,这就是为什么它显示如此.Jus在其背景上设置了一些颜色,你可以看到段的正确框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM