67

我正在尝试更改 UISegmentedcontrol 的高度,但在 Interface Builder 中是不允许的。有什么办法可以改变还是不可能?

谢谢

4

16 回答 16

148

在 IB 中添加约束也可以解决问题:

IB中的约束

于 2013-07-11T20:19:49.883 回答
83

是的,您可以[mySegmentedControl setFrame:frame]在代码中使用。不幸的是,你不能在 IB 中做到这一点。

所以,如果你只想改变高度:

CGRect frame= mySegmentedControl.frame;
[mySegmentedControl setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, fNewHeight)];
于 2012-08-19T15:51:07.497 回答
26

添加一个新的高度约束并设置它的值。

在此处输入图像描述

于 2015-10-27T16:47:52.180 回答
12

如果您正在使用自动布局并且对卢克的回答有疑问,这对我来说非常有效:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:mySegmentedControl
                                     attribute:NSLayoutAttributeHeight 
                                     relatedBy:NSLayoutRelationEqual
                                     toItem:nil
                                     attribute:NSLayoutAttributeNotAnAttribute
                                     multiplier:1 
                                     constant:fNewHeight];
[mySegmentedControl addConstraint:constraint];
于 2013-07-10T06:53:37.927 回答
10

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

在 IB 中添加 frame 属性

于 2014-08-30T21:32:48.890 回答
9

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

在此处输入图像描述

于 2016-12-06T07:20:05.833 回答
7

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
        }
    }
}
于 2017-01-27T07:52:14.790 回答
5

只需转到id UisegmentContrrole所在的情节提要,并使用您的自定义高度设置高度约束

在此处输入图像描述

于 2018-08-28T07:45:26.203 回答
4

快速解决方案:

segmentedControl.frame = CGRect(x: segmentedControl.frame.origin.x, y: segmentedControl.frame.origin.y, width: segmentedControl.frame.size.width, height: 40);

将 SegmentedControl 的高度设置为 40。

于 2015-03-05T20:28:24.513 回答
4

xib幸运的是,您也可以更改高度

您也可以通过 这样做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"/>

在这里,您可以将高度从 44 更改为任何所需的高度。UIConponent你可以改变任何这样的高度。即使对于UIPickerView.

于 2014-03-12T06:34:21.227 回答
2

快速解决方案:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    let rect = CGRect(origin: segment.frame.origin, size: CGSize(width: segment.frame.size.width, height: 100))
    segment.frame = rect
}

或者

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let rect = CGRect(origin: segment.frame.origin, size: CGSize(width: segment.frame.size.width, height: 100))
    segment.frame = rect
}

或者

    override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    let rect = CGRect(origin: segment.frame.origin, size: CGSize(width: segment.frame.size.width, height: 100))
    segment.frame = rect
}

将 SegmentedControl 的高度设置为 100。

于 2016-06-09T04:41:28.547 回答
0

只需MySegmentedControl.frame = CGRectMake(15,5, 290, 50);更改坐标以适合您的视图(这是针对 iOS 7 更新的)

于 2013-12-11T02:04:41.940 回答
0

纪方的解决方案对我不起作用。我必须对分段控件进行子类化并添加这些方法。基于自动布局的解决方案在 iOS7.0 /7.1 中并不健壮。有时它会跳回默认高度。

const static CGFloat kViewHeight = 35;

- (void) layoutSubviews {

    [super layoutSubviews];

    CGRect frame = self.frame;
    [self setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, kViewHeight)];

}

- (CGSize) intrinsicContentSize {

    CGSize defaultSize = [super intrinsicContentSize];
    return CGSizeMake(defaultSize.width, kViewHeight);
}
于 2014-11-20T14:33:29.190 回答
0

您可以尝试以下方法:

segmentedControll.addConstraint(
        NSLayoutConstraint(
            item: segmentedControll,
            attribute: NSLayoutAttribute.Height,
            relatedBy: NSLayoutRelation.Equal,
            toItem: nil,
            attribute: NSLayoutAttribute.NotAnAttribute,
            multiplier: 1,
            constant: 40.0
        )
    )
于 2015-03-26T03:54:16.990 回答
0

如果您选择“使用自动布局”,重置框架不起作用,您应该更改约束。也许这就是最佳答案对某人不起作用的原因,因为在 xcode 5 中默认选择了它

于 2014-09-16T11:59:35.133 回答
-1

对我来说,解决方案是: -

CGRect frame = CGRectMake (20, 20, 180, 50);
self.segmentedControl.frame = frame;

它适用于每个版本,甚至不使用自动调整大小。

于 2014-03-10T17:24:06.040 回答