Several lines of text are displayed after clicking the UISegmentedControl segment, but not initially - UPDATED CODE

I need to set multiple lines of text in each segment UISegmentedControl. I tried the following code and it works fine, but the problem is that when the page loads for the first time and the segmented control is displayed for the first time, it does not display several lines of text, but when I click on one of the segments, the text appears in several lines. How to fix it? Here is the code:

    -(void)configureInitialPage
    {
        //Setting up the segmented control
        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:trends];
        segmentedControl.tintColor = [UIColor colorWithRed:0.03 green:0.28 blue:0.51 alpha:1.0];

        for (id segment in [segmentedControl subviews]) {

            for (id label in [segment subviews]) {

                if ([label isKindOfClass:[UILabel class]]) {

                    UILabel *titleLabel = (UILabel *) label;

                    titleLabel.frame = CGRectMake(0, 0, 97, 50);
                    [titleLabel setFont:[UIFont boldSystemFontOfSize:10]];
                    titleLabel.adjustsFontSizeToFitWidth = YES;
                    titleLabel.numberOfLines = 0;
                }
            }
        }

        [segmentedControl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
        segmentedControl.frame = CGRectMake(20, 510, 720, 40);
        segmentedControl.selectedSegmentIndex = 0;
        [segmentedControl layoutIfNeeded];
        [self.view addSubview:segmentedControl];

}
+2
source share
1 answer

Make [segmentedControl layoutIfNeeded]before adding it as a preview.

+1
source

Source: https://habr.com/ru/post/1648740/


All Articles