单击 UISegmentedControl 的一段后显示多行文本,但最初不是 - 更新代码 [英] Multiple Lines of text shows after a segment of UISegmentedControl is clicked, but not initially - UPDATED CODE

查看:25
本文介绍了单击 UISegmentedControl 的一段后显示多行文本,但最初不是 - 更新代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 UISegmentedControl 的每个段中设置多行文本.我尝试了下面的代码,它工作正常,但问题是当第一次加载页面,并且第一次显示分段控件时,它没有显示多行文本,但是当我点击段之一,文本显示在多行中.如何解决这个问题?代码如下:

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];

}

推荐答案

在将其添加为子视图之前执行 [segmentedControl layoutIfNeeded].

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

这篇关于单击 UISegmentedControl 的一段后显示多行文本,但最初不是 - 更新代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆