【问题标题】:Custom drawing on top of UIButton's background image在 UIButton 背景图像上自定义绘图
【发布时间】:2011-12-15 00:24:00
【问题描述】:

UIButton的绘图方法调用顺序是什么?

我有一个UIButton 子类,我们称之为UIMyButton。我将它添加到 .xib 控制器并给它一个背景图像。然后在 UIMyButton.m 文件中,我重写 drawRect 方法并绘制一个形状。 比如:

- (void)drawRect:(CGRect)rect {
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(c, [UIColor orangeColor].CGColor);
    CGContextFillRect(c, rect);
}

问题在于它在背景图像下方绘制了橙色矩形。哪些方法负责绘制/更新背景图像(例如,它在“点击”时更新)?如何在不向按钮添加额外的自定义子视图(或者背景图像本身是子视图)的情况下强制在顶部绘制?

我只是想清楚地了解UIButton 绘图序列的工作原理。

【问题讨论】:

  • 嗨,巴斯克 - 你在吗?真的,您应该在下面打勾!

标签: objective-c ios cocoa-touch uibutton drawing


【解决方案1】:

解决方案是不为按钮使用背景。相反,在 drawRect 方法中绘制图像,如下所示:

-(void)drawRect:(CGRect)rect
{
    CGContextRef ctx=UIGraphicsGetCurrentContext();

    UIImage *image = [UIImage imageNamed:@"image.jpg"];
    [image drawInRect:self.bounds];

    CGContextSetFillColorWithColor(c, [UIColor orangeColor].CGColor);
    CGContextFillRect(c, rect); 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多