【问题标题】:Subclassing UISegmentedControl to add a colored UIView on each segment子类化 UISegmentedControl 以在每个段上添加彩色 UIView
【发布时间】:2012-05-01 06:29:09
【问题描述】:

我想向 UISegmentedControl 添加一个小彩色指示器(只是一个彩色矩形)。我想我可以继承 UISegmentedControl 并像这样在 initWithFrame 中添加该标志:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        UIColor *first = [UIColor blueColor];
        UIColor *second = [UIColor orangeColor];

        UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.origin.x + 5, self.bounds.origin.y + 5, 10, self.bounds.size.height)];
        firstView.backgroundColor = first;

        [self addSubview:firstView];
    }
    return self;
}

当我实例化一个 CustomSegmentedControl 对象时,我只得到了蓝色矩形,但没有其他原始的 segmentedcontrol 绘图逻辑。有什么想法吗?谢谢。

【问题讨论】:

    标签: iphone uiview uisegmentedcontrol


    【解决方案1】:

    水晶,

    是否可以为彩色矩形使用图像?如果是这样,您可以使用

    - (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment
    

    添加图像。类似的东西

    [segmentedControl setImage:[UIImage imageNamed:@"blueRectangle"] forSegmentAtIndex:0];
    

    希望这会有所帮助!

    【讨论】:

    • 可以为彩色矩形设置图像。我有同样的问题,但我试图摆脱使用一整套.pngs。我也即将子类化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2016-05-24
    • 1970-01-01
    • 2012-09-08
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多