我想改变UISegmentedcontrol的高度,但在Interface Builder中不允许这样做。有没有办法可以修改,还是说不可能实现?
谢谢
我想改变UISegmentedcontrol的高度,但在Interface Builder中不允许这样做。有没有办法可以修改,还是说不可能实现?
谢谢
在IB中添加约束也可以实现这一点:
是的,在代码中您可以使用[mySegmentedControl setFrame:frame]
。不幸的是,您无法在IB中执行此操作。
因此,如果您只想更改高度:
CGRect frame= mySegmentedControl.frame;
[mySegmentedControl setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, fNewHeight)];
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:mySegmentedControl
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:fNewHeight];
[mySegmentedControl addConstraint:constraint];
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
}
}
}
解决方案:
SwiftsegmentedControl.frame = CGRect(x: segmentedControl.frame.origin.x, y: segmentedControl.frame.origin.y, width: segmentedControl.frame.size.width, height: 40);
将 SegmentedControl 的高度设置为 40。
幸运的是,你也可以从xib
中更改高度.
你可以通过xib
来实现。只需将分段控件添加到xib
中。然后在TextEdit中打开xib
。你会找到以下代码:
<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
。
CGSize defaultSize = [super intrinsicContentSize]; return CGSizeMake(defaultSize.width, 35);
}高度为35(因此NSContentSizeLayoutConstraint的高度与NSLayoutConstraint相同),但仍然会出现控件有时跳回默认高度28的情况。 - Vladimír Slavík