iOS: change the height of UISegmentedcontrol
IosUisegmentedcontrolIos Problem Overview
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?
Thanks
Ios Solutions
Solution 1 - Ios
Adding a constraint in IB will also do the trick:
Solution 2 - Ios
Yes, you can use [mySegmentedControl setFrame:frame]
in code. It is unfortunate that you can't do this in 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)];
Solution 3 - Ios
Solution 4 - Ios
If you are using Auto Layout and are having trouble with Luke's answer, this worked perfectly for me:
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:mySegmentedControl
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:fNewHeight];
[mySegmentedControl addConstraint:constraint];
Solution 5 - Ios
To do it inside Interface Builder you can select the control and add frame attribute under "User Defined Runtime Attributes"
Solution 6 - Ios
Best and simple way to do is set height constraint to segmented control and increase its value as you needed.
Solution 7 - Ios
Swift 3 IBInspectable Solution :
@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
}
}
}
Solution 8 - Ios
Just Go to Storyboard where id UisegmentContrrole is located and set height constraint with your custom heights as
Solution 9 - Ios
Fortunately you can change the height from the xib
as well.
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 :
<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
.
Solution 10 - Ios
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.
Solution 11 - Ios
Swift Solution:
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
}
or
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let rect = CGRect(origin: segment.frame.origin, size: CGSize(width: segment.frame.size.width, height: 100))
segment.frame = rect
}
or
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let rect = CGRect(origin: segment.frame.origin, size: CGSize(width: segment.frame.size.width, height: 100))
segment.frame = rect
}
Sets the height of the SegmentedControl to 100.
Solution 12 - Ios
Just do MySegmentedControl.frame = CGRectMake(15,5, 290, 50);
and just change coordinates to fit it for your view
(This is updated for iOS 7)
Solution 13 - Ios
If you select the "Use Auto Layout" , reset frame doesn't work, you should change the constraints instead. Perhaps it is the reason why the best answer not work for somebody , because it is selected by default in xcode 5
Solution 14 - Ios
The solution from Ji Fang did not work for me. I had to subclass the segmented control and add these methods. The solution based on auto layout is not robust in iOS7.0 /7.1. Sometimes it jumps back to the default height.
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);
}
Solution 15 - Ios
You can try out the following:
segmentedControll.addConstraint(
NSLayoutConstraint(
item: segmentedControll,
attribute: NSLayoutAttribute.Height,
relatedBy: NSLayoutRelation.Equal,
toItem: nil,
attribute: NSLayoutAttribute.NotAnAttribute,
multiplier: 1,
constant: 40.0
)
)
Solution 16 - Ios
For me solution was:-
CGRect frame = CGRectMake (20, 20, 180, 50);
self.segmentedControl.frame = frame;
It works in every version and without even using autoresizing.