IOS: UISegmentedcontrol height change

I am trying to change the height of a UISegmentedcontrol, but this is not allowed in Interface Builder. Is there a way to change or is this impossible?

thank

+63
ios uisegmentedcontrol
Aug 19 '12 at 15:17
source share
17 answers

Yes, you can use [mySegmentedControl setFrame:frame] in your code. Unfortunately, you cannot 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)]; 
+82
Aug 19 '12 at 15:51
source share
— -

Adding a constraint to IB will also do the trick:

Constraint in ib

+137
Jul 11 '13 at 20:19
source share

Add a new height limit and set its value.

enter image description here

+22
Oct 27 '15 at 16:47
source share

If you use auto-layout and have problems with Luke answer , this worked fine for me:

 NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:mySegmentedControl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:fNewHeight]; [mySegmentedControl addConstraint:constraint]; 
+12
Jul 10 '13 at 6:53 on
source share

To do this inside Interface Builder, you can select a control and add a frame in the section "Attributes of custom runtime attributes"

add frame attribute inside IB

+10
Aug 30 '14 at 21:32
source share

The best and easiest way is to set a height limit for segmented control and increase its value as necessary.

enter image description here

+8
Dec 06 '16 at 7:20
source share

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 } } } 
+7
Jan 27 '17 at 7:52
source share

Fortunately, you can also change the height with xib .

You can do this with xib . Just add a segment control to xib . Then open 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 desired height. You can change the height of any UIConponent as follows. Even for UIPickerView .

+4
Mar 12 '14 at 6:34
source share

Fast decision:

 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 segment to 40.

+2
Mar 05 '15 at 20:28
source share

Just go to the storyboard where the id of UisegmentContrrole is located and set a height limit with your custom heights, like

enter image description here

+2
Aug 28 '18 at 7:45
source share

Fast decision:

 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.

+1
Jun 09 '16 at 4:41
source share

Just do MySegmentedControl.frame = CGRectMake(15,5, 290, 50); and just change the coordinates to fit your view (This is an update for iOS 7)

0
Dec 11 '13 at 2:04 on
source share

For me, the solution was: -

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

It works in every version and does not even use an autoresistor.

0
Mar 10 '14 at 17:24
source share

If you select Use Auto Layout, the reset frame does not work, you should change the restrictions instead. Perhaps that is why the best answer does not work for someone, because it is selected by default in xcode 5

0
Sep 16 '14 at 11:59
source share

The solution from Ji Fang did not work for me. I had to subclass a segmented control and add these methods. The solution based on automatic layout is not reliable in iOS7.0 / 7.1. Sometimes it returns 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); } 
0
Nov 20 '14 at 2:33
source share

You can try the following:

 segmentedControll.addConstraint( NSLayoutConstraint( item: segmentedControll, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40.0 ) ) 
0
Mar 26 '15 at 3:54
source share
 - (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment { [super setTitle:title forSegmentAtIndex:segment]; //正常展示 多行[self setNeedsLayout]; for(UIView *subview in self.subviews) { if([NSStringFromClass(subview.class) isEqualToString:@"UISegment"]) { for(UIView *segmentSubview in subview.subviews) { if([NSStringFromClass(segmentSubview.class) isEqualToString:@"UISegmentLabel"]) { UILabel *label = (id)segmentSubview; label.numberOfLines = 2; CGRect frame = label.frame; frame.size = label.superview.frame.size; label.frame = frame; } } } } [self layoutIfNeeded]; 

}

0
Dec 10 '18 at 8:05
source share



All Articles