Is it possible to create multi line UISegmentedControl?

后端 未结 5 451
不知归路
不知归路 2020-12-11 05:21

I have a relative longer text items in my segmented control so I need to break text at certain points. Is it possible to use line breaks? I know at buttons

5条回答
  •  孤城傲影
    2020-12-11 06:00

    if you have a standard UISegmentedControl you can use the following idea:

    [_segmentedControl.subviews enumerateObjectsUsingBlock:^(UIView * obj, NSUInteger idx, BOOL *stop) {
        [obj.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if ([obj isKindOfClass:[UILabel class]]) {
                UILabel *_tempLabel = (UILabel *)obj;
                [_tempLabel setNumberOfLines:0];
            }
        }];     
    }];
    

    you may need to set the height of your instance as well.


    NOTE: I need to add a little warning about – as rmaddy has also pointed out correctly – this is a quite fragile solution, because if the segmented control's view hierarchy would be changed in the future iOS versions that code may not work properly anymore.

提交回复
热议问题