iOS:更改 UISegmentedcontrol 的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12027608/
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
iOS: change the height of UISegmentedcontrol
提问by DavidNg
I am trying to change the height of UISegmentedcontrol, but it is not allowed in the Interface Builder. Is there any way to change or it is impossible?
我正在尝试更改 UISegmentedcontrol 的高度,但在 Interface Builder 中不允许这样做。有没有办法改变或者不可能?
Thanks
谢谢
回答by Ji Fang
Adding a constraint in IB will also do the trick:
在 IB 中添加约束也可以解决问题:
回答by Luke
Yes, you can use [mySegmentedControl setFrame:frame]
in code. It is unfortunate that you can't do this in IB.
是的,您可以[mySegmentedControl setFrame:frame]
在代码中使用。不幸的是,你不能在 IB 中做到这一点。
So, if you just want to change the height:
因此,如果您只想更改高度:
CGRect frame= mySegmentedControl.frame;
[mySegmentedControl setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, fNewHeight)];
回答by jburns20
If you are using Auto Layout and are having trouble with Luke's answer, this worked perfectly for me:
如果您正在使用 Auto Layout 并且在Luke's answer遇到问题,这对我来说非常有效:
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:mySegmentedControl
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:fNewHeight];
[mySegmentedControl addConstraint: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 中执行此操作,您可以选择控件并在“用户定义的运行时属性”下添加框架属性
回答by Arshad Shaik
回答by Kew Kylyu Vacquier
Swift 3 IBInspectable Solution :
Swift 3 IBInspectable 解决方案:
@IBDesignable class MySegmentedControl: UISegmentedControl {
@IBInspectable var height: CGFloat = 29 {
didSet {
let centerSave = center
frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: height)
center = centerSave
}
}
}
回答by Rahul Panzade
回答by NiKKi
Fortunately you can change the height from the xib
as well.
幸运的是,你可以从改变高度xib
和。
You can do so through the xib
as well. Just add a segment control into the xib
. Then open the xib
in TextEdit. There you will find the code :
您也可以通过 这样做xib
。只需在xib
. 然后xib
在 TextEdit 中打开。在那里你会找到代码:
<segmentedControl opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="B99-Tt-vJG">
<rect key="frame" x="222" y="82" width="123" height="44"/>
Here you change the height from 44 to any required height. You can change the height of any UIConponent
like this. Even for the UIPickerView
.
在这里,您可以将高度从 44 更改为任何所需的高度。您可以UIConponent
像这样更改任何高度。即使对于UIPickerView
.
回答by datayeah
Swift Solution:
迅捷解决方案:
segmentedControl.frame = CGRect(x: segmentedControl.frame.origin.x, y: segmentedControl.frame.origin.y, width: segmentedControl.frame.size.width, height: 40);
sets the height of the SegmentedControl to 40.
将 SegmentedControl 的高度设置为 40。