Change Height of UISegmentedControl

后端 未结 5 1360
小鲜肉
小鲜肉 2021-01-14 09:14

I am trying to change height of UISegmentedControl using this code:

CGRect frame= mySegmentedControl.frame;
[mySegmentedControl setFrame:CGRectM         


        
相关标签:
5条回答
  • 2021-01-14 09:44

    For me i'm using this mySegmentedControl.frame=CGRectMake(236, 178, 171, 35); and it works like a charme.

    Did you check the frame values?

    0 讨论(0)
  • 2021-01-14 09:49

    To do it inside Interface Builder you can select the control and add frame attribute under "User Defined Runtime Attributes"

    adding frame attribute inside Interface Builder

    0 讨论(0)
  • 2021-01-14 09:54

    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.

    0 讨论(0)
  • 2021-01-14 09:55

    Seems it's about the view cycle.

    Laying out views in iOS actually concerning 3 factors 1. autolayout (avaliable since iOS6) 2. setFrame 3. autoreiszing mask

    For your case,

    autolayout will help to calculate the required frame (based on the NSLayouConstraints)for that UIView then lay them out.

    So in the interface builder, if you enable "autolayout", the interface builder is generating NsLayoutConstraints for that UIView. if you disable "autolayout" , the builder is generating a frame for you UIView.

    Assuming you have to use autolayout, you have to resize your UISegmentedControl in a correct timing

    - (void)viewDidLayoutSubview
    

    It's about the view life cycle. To simplify it, it's like:

    updateConstraints -> layout (You better watch related WWDC Session Videos for details)

    You can't change the frame because this flow:

    set new frame by code -> view handle constraints and calculation(generating an other frame for the UISegmentedControl) -> do the layout

    Sorry for this not-well-organised answer.

    0 讨论(0)
  • 2021-01-14 10:00

    Perhaps this will help: I had a custom-sized UISegmentedControl within a Storyboard view. The Storyboard views were utilizing AutoLayout and each time I pressed one of the segments, the UISegmentedControl would resize to the default height and not to the custom frame size.

    My solution:

    1) Set the UISegmentedControl frame in -(void)viewDidLoad:

    [mySegControl setFrame:CGRectMake(100, 170, 568, 100)];
    

    2) Set the frame in -(void)viewDidLayoutSubviews:

    [mySegControl setFrame:CGRectMake(100, 170, 568, 100)];
    

    3) In the method you use as the target for when a segment is selected, call setNeedsLayout. This will force the UISegmentedControl to be re-laid-out IAW your custom frame size

    [self.view setNeedsLayout];
    
    0 讨论(0)
提交回复
热议问题