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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:46:56  来源:igfitidea点击:

iOS: change the height of UISegmentedcontrol

iosuisegmentedcontrol

提问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 中添加约束也可以解决问题:

Constraint in IB

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 naveed148

Add a new height constraint and set its value.

添加新的高度约束并设置其值。

enter image description here

在此处输入图片说明

回答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 中执行此操作,您可以选择控件并在“用户定义的运行时属性”下添加框架属性

add frame attribute inside IB

在 IB 中添加 frame 属性

回答by Arshad Shaik

Best and simple way to do is set height constraint to segmented control and increase its value as you needed.

最好和最简单的方法是将高度约束设置为分段控件并根据需要增加其值。

enter image description here

在此处输入图片说明

回答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

Just Go to Storyboardwhere id UisegmentContrrole is located and set height constraintwith your custom heightsas

只要到故事板其中id UisegmentContrrole位于并设置高度约束与自定义的高度

enter image description here

在此处输入图片说明

回答by NiKKi

Fortunately you can change the height from the xibas well.

幸运的是,你可以从改变高度xib

You can do so through the xibas well. Just add a segment control into the xib. Then open the xibin 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 UIConponentlike 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。