Multiple Lines of text shows after a segment of UISegmentedControl is clicked, but not initially - UPDATED CODE

℡╲_俬逩灬. 提交于 2019-12-23 17:14:50

问题


I need to set multiple lines of text in each segment of a UISegmentedControl. I tried the following code and it works fine, but the problem is that when the page is loaded for the first time, and the segmented control is shown for the first time, it does not show multiple lines of text, but when i click on one of the segments, the text shows up in multiple line. How to fix this? Here's 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];

}

回答1:


Do [segmentedControl layoutIfNeeded] before adding it as a subview.



来源:https://stackoverflow.com/questions/22165171/multiple-lines-of-text-shows-after-a-segment-of-uisegmentedcontrol-is-clicked-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!