UITableViewCell圆角和剪辑子视图 [英] UITableViewCell rounded corners and clip subviews

查看:165
本文介绍了UITableViewCell圆角和剪辑子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何地方(搜索引擎,文档,这里等),它们显示了如何在一个也剪辑子视图的元素上创建圆角(特别是在分组表视图中)。

I can't find anything anywhere(search engines, docs, here, etc) that shows how to create rounded corners (esp. in a grouped table view) on an element that also clips the subviews.

我的代码可以正确地创建一个带有4个圆弧(圆角)的路径中的圆角矩形,这个圆弧已在我的子类uitableviewcell中的drawRect:方法中测试过。问题是,子视图恰好是带有内部uiimageviews的uibutton,不遵守uitableviewcell服从的CGContextClip()。

I have code that properly creates a rounded rectangle out of a path with 4 arcs(the rounded corners) that has been tested in the drawRect: method in my subclassed uitableviewcell. The issue is that the subviews, which happen to be uibuttons with their internal uiimageviews, do no obey the CGContextClip() that the uitableviewcell obeys.

这是代码:

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

    CGFloat radius = 12;
    CGFloat width = CGRectGetWidth(rect);
    CGFloat height = CGRectGetHeight(rect);

    // Make sure corner radius isn't larger than half the shorter side
    if (radius > width/2.0)
        radius = width/2.0;
    if (radius > height/2.0)
        radius = height/2.0;    

    CGFloat minx = CGRectGetMinX(rect) + 10;
    CGFloat midx = CGRectGetMidX(rect);
    CGFloat maxx = CGRectGetMaxX(rect) - 10;
    CGFloat miny = CGRectGetMinY(rect);
    CGFloat midy = CGRectGetMidY(rect);
    CGFloat maxy = CGRectGetMaxY(rect);

    [[UIColor greenColor] set];


    CGContextBeginPath(context);

    CGContextMoveToPoint(context, minx, midy);

    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);

    CGContextClip(context);
    CGContextFillRect(context, rect);

    [super drawRect:rect];    
}

因为这个特定情况是静态的(仅显示在1个特定的按钮行中) ,我可以编辑用于按钮的图像以获得所需的效果。

Because this specific case is static(only shows in 1 specific row of buttons), I can edit the images being used for the buttons to get the desired effect.

HOWEVER ,我有另一个动态的案例。具体来说,一个分组表,其中包含许多数据库驱动的结果,这些结果将显示可能位于第一行或最后一行的带有圆角的照片,因此需要进行剪裁。

HOWEVER, I have another case that is dynamic. Specifically, a grouped table with lots of database-driven results that will show photos that may be in the first or last row with rounded corners and thus needs to be clipped).

那么, 是否可以创建一个也剪辑子视图的CGContextClip()?如果是这样,怎么做?

So, is it possible to create a CGContextClip() that also clips the subviews? If so, how?

推荐答案

创建 UIImageView的子类带圆角和透明度。 UITableViewCell 本身应该是不透明的以获得更好的性能。

Create a subclass of UIImageView with rounded corners and transparency. The UITableViewCell itself should be opaque for better performance.

看看这个例子

这篇关于UITableViewCell圆角和剪辑子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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