问题
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