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
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
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)];
Adding a constraint in IB will also do the trick:
Add a new height constraint and set its value.
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];
To do it inside Interface Builder you can select the control and add frame attribute under "User Defined Runtime Attributes"
Best and simple way to do is set height constraint to segmented control and increase its value as you needed.
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
}
}
}
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
.
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.
Just Go to Storyboard where id UisegmentContrrole is located and set height constraint with your custom heights as
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.
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)
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.
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);
}
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
)
)
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