【问题标题】:Round two corners in UIViewUIView中的圆两个角
【发布时间】:2011-06-18 08:15:39
【问题描述】:

不久前,我发布了一个关于rounding just two corners of a view 的问题,得到了很好的回应,但在实现它时遇到了问题。这是我的 drawRect: 方法:

- (void)drawRect:(CGRect)rect {
    //[super drawRect:rect]; <------Should I uncomment this?
    int radius = 5;
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, M_PI, M_PI / 2, 1);
    CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
    CGContextClosePath(context);
    CGContextClip(context);
}

正在调用该方法,但似乎不会影响视图的结果。任何想法为什么?

【问题讨论】:

    标签: ios objective-c iphone rounded-corners


    【解决方案1】:

    CACornerMask 在 iOS 11 中引入,有助于在视图层中定义 topleft、topright、bottomleft、bottom right。以下是使用示例。

    这里我尝试只圆两个顶角:

    myView.clipsToBounds = true
    myView.layer.cornerRadius = 10
    myView.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
    

    仅供参考:

    【讨论】:

    • 这看起来像是现在的惯用方式。好收获!
    • 请注意,视图的宽度/高度仍然需要至少是角半径的两倍,否则会出现奇怪的伪影。例如仅将底角四舍五入,半径为 20 且高度仅为 30 会导致单元格明显变窄(一两个像素)。
    • 请注意,如果您的视图更改其框架(可能是由于 UITextView 的扩展,如我的情况 - stackoverflow.com/questions/50926215/… ),这是正确的方法。如果你设置了 UIBezierPath,它不会在框架发生变化时自动更新。
    • 很好的答案。原来将clipsToBounds 设置为true 是不必要的。当clipsToBoundsfalse 时,您可以遮盖角落并向图层添加阴影。对我有用。
    • 完美!为我工作
    【解决方案2】:


    据我所知,如果您还需要屏蔽子视图,可以使用CALayer 屏蔽。有两种方法可以做到这一点。第一个更优雅一点,第二个是一种解决方法:-) 但它也很快。两者都基于CALayer 屏蔽。去年我在几个项目中使用了这两种方法,希望你能找到有用的东西。

    解决方案 1

    首先,我创建了这个函数来动态生成一个带有我需要的圆角的图像蒙版 (UIImage)。这个函数本质上需要 5 个参数:图像的边界和 4 个角半径(左上、右上、左下和右下)。

    
    static inline UIImage* MTDContextCreateRoundedMask( CGRect rect, CGFloat radius_tl, CGFloat radius_tr, CGFloat radius_bl, CGFloat radius_br ) {  
    
        CGContextRef context;
        CGColorSpaceRef colorSpace;
    
        colorSpace = CGColorSpaceCreateDeviceRGB();
    
        // create a bitmap graphics context the size of the image
        context = CGBitmapContextCreate( NULL, rect.size.width, rect.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast );
    
        // free the rgb colorspace
        CGColorSpaceRelease(colorSpace);    
    
        if ( context == NULL ) {
            return NULL;
        }
    
        // cerate mask
    
        CGFloat minx = CGRectGetMinX( rect ), midx = CGRectGetMidX( rect ), maxx = CGRectGetMaxX( rect );
        CGFloat miny = CGRectGetMinY( rect ), midy = CGRectGetMidY( rect ), maxy = CGRectGetMaxY( rect );
    
        CGContextBeginPath( context );
        CGContextSetGrayFillColor( context, 1.0, 0.0 );
        CGContextAddRect( context, rect );
        CGContextClosePath( context );
        CGContextDrawPath( context, kCGPathFill );
    
        CGContextSetGrayFillColor( context, 1.0, 1.0 );
        CGContextBeginPath( context );
        CGContextMoveToPoint( context, minx, midy );
        CGContextAddArcToPoint( context, minx, miny, midx, miny, radius_bl );
        CGContextAddArcToPoint( context, maxx, miny, maxx, midy, radius_br );
        CGContextAddArcToPoint( context, maxx, maxy, midx, maxy, radius_tr );
        CGContextAddArcToPoint( context, minx, maxy, minx, midy, radius_tl );
        CGContextClosePath( context );
        CGContextDrawPath( context, kCGPathFill );
    
        // Create CGImageRef of the main view bitmap content, and then
        // release that bitmap context
        CGImageRef bitmapContext = CGBitmapContextCreateImage( context );
        CGContextRelease( context );
    
        // convert the finished resized image to a UIImage 
        UIImage *theImage = [UIImage imageWithCGImage:bitmapContext];
        // image is retained by the property setting above, so we can 
        // release the original
        CGImageRelease(bitmapContext);
    
        // return the image
        return theImage;
    }  
    

    现在您只需要几行代码。我把东西放在我的 viewController viewDidLoad 方法中,因为它更快,但您也可以在自定义 UIView 中使用它,例如 layoutSubviews 方法。

    
    
    - (void)viewDidLoad {
    
        // Create the mask image you need calling the previous function
        UIImage *mask = MTDContextCreateRoundedMask( self.view.bounds, 50.0, 50.0, 0.0, 0.0 );
        // Create a new layer that will work as a mask
        CALayer *layerMask = [CALayer layer];
        layerMask.frame = self.view.bounds;       
        // Put the mask image as content of the layer
        layerMask.contents = (id)mask.CGImage;       
        // set the mask layer as mask of the view layer
        self.view.layer.mask = layerMask;              
    
        // Add a backaground color just to check if it works
        self.view.backgroundColor = [UIColor redColor];
        // Add a test view to verify the correct mask clipping
        UIView *testView = [[UIView alloc] initWithFrame:CGRectMake( 0.0, 0.0, 50.0, 50.0 )];
        testView.backgroundColor = [UIColor blueColor];
        [self.view addSubview:testView];
        [testView release];
    
        [super viewDidLoad];
    }
    
    

    解决方案 2

    这个解决方案有点“脏”。本质上,您可以创建一个具有所需圆角(所有角)的蒙版层。然后你应该通过角半径的值来增加遮罩层的高度。这样,底部圆角被隐藏,您只能看到上部圆角。我将代码放在viewDidLoad 方法中,因为它更快,但您也可以在自定义UIView 中使用它,例如layoutSubviews 方法。

      
    
    - (void)viewDidLoad {
    
        // set the radius
        CGFloat radius = 50.0;
        // set the mask frame, and increase the height by the 
        // corner radius to hide bottom corners
        CGRect maskFrame = self.view.bounds;
        maskFrame.size.height += radius;
        // create the mask layer
        CALayer *maskLayer = [CALayer layer];
        maskLayer.cornerRadius = radius;
        maskLayer.backgroundColor = [UIColor blackColor].CGColor;
        maskLayer.frame = maskFrame;
    
        // set the mask
        self.view.layer.mask = maskLayer;
    
        // Add a backaground color just to check if it works
        self.view.backgroundColor = [UIColor redColor];
        // Add a test view to verify the correct mask clipping
        UIView *testView = [[UIView alloc] initWithFrame:CGRectMake( 0.0, 0.0, 50.0, 50.0 )];
        testView.backgroundColor = [UIColor blueColor];
        [self.view addSubview:testView];
        [testView release];
    
        [super viewDidLoad];
    }
    
    

    希望这会有所帮助。咻!

    【讨论】:

    • 终于成功了!非常感谢!一个比另一个有什么优势?
    • 无论如何,即使是 CAShapeLayer 解决方案也是完美的。您可以使用 UIBezierPath(需要 3.2+)或仅使用 CGPath 方法(CGPathMoveToPoint、CGPathAddQuadCurveToPoint 等)绘制 CGPath
    • 如何让蒙版在从纵向旋转到横向后自动调整大小?
    • 别忘了添加 layer.masksToBounds = YES
    • 如何做到只圆顶和右下角?
    【解决方案3】:

    通过几个答案和cmets,我发现使用UIBezierPath bezierPathWithRoundedRectCAShapeLayer 是最简单和最直接的方法。它可能不适合非常复杂的情况,但对于偶尔的圆角,它对我来说工作得又快又顺利。

    我创建了一个简化的助手,用于设置掩码中的适当角:

    -(void) setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
    {
        UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(10.0, 10.0)];
    
        CAShapeLayer* shape = [[CAShapeLayer alloc] init];
        [shape setPath:rounded.CGPath];
    
        view.layer.mask = shape;
    }
    

    要使用它,只需使用适当的 UIRectCorner 枚举调用,例如:

    [self setMaskTo:self.photoView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft];
    

    请注意,对我来说,我使用它来圆角分组 UITableViewCell 中的照片,10.0 半径对我来说很好,如果需要适当地更改值。

    编辑: 请注意之前的回答与此非常相似 (link)。如果需要,您仍然可以将此答案用作附加的便利功能。


    编辑: 与 Swift 3 中的 UIView 扩展相同的代码
    extension UIView {
        func maskByRoundingCorners(_ masks:UIRectCorner, withRadii radii:CGSize = CGSize(width: 10, height: 10)) {
            let rounded = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: masks, cornerRadii: radii)
    
            let shape = CAShapeLayer()
            shape.path = rounded.cgPath
    
            self.layer.mask = shape
        }
    }
    

    要使用它,只需在任何UIView 上调用maskByRoundingCorner

    view.maskByRoundingCorners([.topLeft, .bottomLeft])
    

    【讨论】:

    • 在添加边框的同时可以这样做吗?
    • 我不得不将函数调用放入 DispatchQueue.main.async {},因为只有一个角是圆的...
    • @MaksimKniazev 是的,这必须从主(UI)线程调用。此规则适用于任何接触 UI 的操作,它们必须从主线程调用,否则可能会发生一些奇怪的事情。
    • 似乎只有一个角是圆的。尝试过@MaksimKniazev 解决方案也没有用。
    • 我和@Gustavo_fringe 有同样的问题,右上角没有被剪裁/屏蔽到 xib 视图内部(调整大小)的边界,不知道如何修复它。
    【解决方案4】:

    我无法在对@lomanf 的回答的评论中满足所有这些要求。所以我将其添加为答案。

    就像@lomanf 所说,您需要添加图层蒙版以防止子图层在路径边界之外绘制。不过,现在做起来容易多了。只要您的目标是 iOS 3.2 或更高版本,您就不需要使用石英创建图像并将其设置为蒙版。您可以简单地创建一个带有UIBezierPathCAShapeLayer 并将其用作掩码。

    此外,在使用图层蒙版时,请确保在添加蒙版时要蒙版的图层不属于任何图层层次结构。否则行为未定义。如果您的视图已经在层次结构中,则需要将其从其父视图中删除,屏蔽它,然后将其放回原来的位置。

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    UIBezierPath *roundedPath = 
      [UIBezierPath bezierPathWithRoundedRect:maskLayer.bounds
                            byRoundingCorners:UIRectCornerTopLeft |
                                              UIRectCornerBottomRight
                                  cornerRadii:CGSizeMake(16.f, 16.f)];    
    maskLayer.fillColor = [[UIColor whiteColor] CGColor];
    maskLayer.backgroundColor = [[UIColor clearColor] CGColor];
    maskLayer.path = [roundedPath CGPath];
    
    //Don't add masks to layers already in the hierarchy!
    UIView *superview = [self.view superview];
    [self.view removeFromSuperview];
    self.view.layer.mask = maskLayer;
    [superview addSubview:self.view];
    

    由于 Core Animation 渲染的工作方式,遮罩是一个相对较慢的操作。每个蒙版都需要一个额外的渲染通道。所以要少戴口罩。

    这种方法最好的部分之一是您不再需要创建自定义UIView 并覆盖drawRect:。这应该会使您的代码更简单,甚至可能更快。

    【讨论】:

    • 看起来很有希望,但视图无法显示。任何想法为什么?
    • 您正在使用 maskLayer.bounds 来调整 UIBezierPath 的大小,但边界的值是 CGRectZero。您需要添加第二行代码(在 CAShapeLayer 初始化之后立即),例如 maskLayer.frame = self.view.bounds;
    • 是的,你是对的。我从另一个已经存在形状层的示例中提取了该代码。对此感到抱歉。
    • 完美运行!添加 maskLayer.frame = self.view.bounds,如上所述,make 是可行的。否则视图不会显示。
    • +1 这很好用。事实上,我认为我更喜欢这种方法而不是 lomanf 的 #2 解决方案。这种方法很容易让您在对角添加圆弧(例如:右上角 + 左下角),而且它肯定比 lomanf 的第一个解决方案短。伟大的代码弥敦道!
    【解决方案5】:

    我以 Nathan 为例并在 UIView 上创建了一个类别,以允许人们遵守 DRY 原则。废话不多说:

    UIView+Roundify.h

    #import <UIKit/UIKit.h>
    
    @interface UIView (Roundify)
    
    -(void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii;
    -(CALayer*)maskForRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii;
    
    @end
    

    UIView+Roundify.m

    #import "UIView+Roundify.h"
    
    @implementation UIView (Roundify)
    
    -(void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii {
        CALayer *tMaskLayer = [self maskForRoundedCorners:corners withRadii:radii];
        self.layer.mask = tMaskLayer;
    }
    
    -(CALayer*)maskForRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii {
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.bounds;
    
        UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect:
            maskLayer.bounds byRoundingCorners:corners cornerRadii:radii];
        maskLayer.fillColor = [[UIColor whiteColor] CGColor];
        maskLayer.backgroundColor = [[UIColor clearColor] CGColor];
        maskLayer.path = [roundedPath CGPath];
    
        return maskLayer;
    }
    
    @end
    

    打电话:

    [myView addRoundedCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
                    withRadii:CGSizeMake(20.0f, 20.0f)];
    

    【讨论】:

    • 不要忘记在 .m 文件中导入 并将 QuartzCore.framework 添加到项目中
    • +1 好东西,不过有一个问题,删除/读取视图到它的超级视图的目的是什么?
    • @eladleb:这里的基本代码来自 Nathan,我只是稍微优化了一下。根据我所做的研究,我找不到在应用图层蒙版之前删除视图的正当理由。因此,我认为省略该代码是安全的。
    • @FreeAsInBeer - 我同意,然后我会投票删除它,这实际上会改变视图 z 顺序并会产生更多问题..
    • 解决了我在向 TableView 添加圆角时遇到的问题:我添加了页眉、圆角顶角并添加了带有圆角底角的页脚。谢谢。
    【解决方案6】:

    为了稍微扩展 P.L 的回答,我重写了该方法,因为它没有正确舍入某些对象,例如 UIButton

    - (void)setMaskTo:(id)sender byRoundingCorners:(UIRectCorner)corners withCornerRadii:(CGSize)radii
    {
        // UIButton requires this
        [sender layer].cornerRadius = 0.0;
    
        UIBezierPath *shapePath = [UIBezierPath bezierPathWithRoundedRect:[sender bounds]
                                                    byRoundingCorners:corners
                                                    cornerRadii:radii];
    
        CAShapeLayer *newCornerLayer = [CAShapeLayer layer];
        newCornerLayer.frame = [sender bounds];
        newCornerLayer.path = shapePath.CGPath;
        [sender layer].mask = newCornerLayer;
    }
    

    然后调用它

    [self setMaskTo:self.continueButton byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight withCornerRadii:CGSizeMake(3.0, 3.0)];
    

    【讨论】:

      【解决方案7】:

      如果您想在 Swift 中执行此操作,您可以使用 UIView 的扩展名。通过这样做,所有子类将能够使用以下方法:

      import QuartzCore
      
      extension UIView {
          func roundCorner(corners: UIRectCorner, radius: CGFloat) {
              let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
              let maskLayer = CAShapeLayer()
              maskLayer.frame = bounds
              maskLayer.path = maskPath.CGPath
              layer.mask = maskLayer
          }
      }    
      

      示例用法:

      self.anImageView.roundCorner(.topRight, radius: 10)
      

      【讨论】:

        【解决方案8】:

        扩展已接受的答案,让我们为其添加向后兼容性。在 iOS 11 之前,view.layer.maskedCorners 不可用。所以我们可以这样做

        if #available(iOS 11.0, *) {
            myView.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
        } else {
            myView.maskByRoundingCorners([.topLeft, .topRight])
        }
        
        extension UIView{
            func maskByRoundingCorners(_ masks:UIRectCorner, withRadii radii:CGSize = CGSize(width: 10, height: 10)) {
                let rounded = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: masks, cornerRadii: radii)
        
                let shape = CAShapeLayer()
                shape.path = rounded.cgPath
        
                self.layer.mask = shape
            }
        }
        

        我们将 maskByRoundingCorners 编写为 UIView 扩展,以便提高代码重用性。

        感谢@SachinVsSachin 和@P.L :) 我结合了他们的代码以使其更好。

        【讨论】:

          【解决方案9】:
          UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(5, 5, self.bounds.size.width-10, self.bounds.size.height-10)
                                                         byRoundingCorners:UIRectCornerAllCorners
                                                               cornerRadii:CGSizeMake(12.0, 12.0)];
          

          根据需要更改“AllCorners”

          【讨论】:

          • 那么您必须在 UIView 中执行此操作还是可以在 UIViewController 中执行此操作?一旦你做了 UIBezierPath,你会怎么做?谢谢。
          【解决方案10】:

          提供的所有解决方案都实现了目标。但是,UIConstraints 有时会搞砸。

          例如,底角需要倒圆角。如果身高或 底部间距约束设置为需要四舍五入的UIView, 需要将圆角的代码 sn-ps 移动到 viewDidLayoutSubviews 方法。

          突出显示:

          UIBezierPath *maskPath = [UIBezierPath
              bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:
              (UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(16.0, 16.0)];
          

          如果此代码设置在viewDidLoad 中,则上面的代码 sn-p 只会使右上角变圆。因为roundedView.bounds 将在约束更新UIView 后发生变化。

          【讨论】:

            【解决方案11】:

            创建一个遮罩并将其设置在视图的图层上

            【讨论】:

            【解决方案12】:

            从您的代码开始,您可能会使用下面的 sn-p 之类的东西。

            我不确定这是否是您想要的结果。同样值得注意的是,如果/当系统再次调用 drawRect: 时,只要求重绘矩形的一部分,这将表现得非常奇怪。 Nevan's approach,如上所述,可能是一个更好的方法。

             // make sure the view's background is set to [UIColor clearColor]
            - (void)drawRect:(CGRect)rect
            {
                CGFloat radius = 10.0;
                CGContextRef context = UIGraphicsGetCurrentContext();
            
                CGContextTranslateCTM(context, rect.size.width/2, rect.size.height/2);
                CGContextRotateCTM(context, M_PI); // rotate so image appears right way up
                CGContextTranslateCTM(context, -rect.size.width/2, -rect.size.height/2);
            
                CGContextBeginPath(context);
            
                CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
                CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, M_PI, M_PI / 2, 1);
                CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
                CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y);
                CGContextClip(context); 
            
                // now do your drawing, e.g. draw an image
                CGImageRef anImage = [[UIImage imageNamed:@"image.jpg"] CGImage];
                CGContextDrawImage(context, rect, anImage);    
            }
            

            【讨论】:

            • 我设计了一个圆角的视图,但我不能让它剪裁它的任何子视图。如果我在视图边缘放置一个带有方角的视图,它仍然会覆盖其父视图的圆角。知道为什么会这样吗?
            • 因为-drawRect: 中的剪辑只会影响您正在绘制的视图。绘制完成后,它的子视图独立绘制。
            【解决方案13】:

            一个稍微老套但相对简单(没有子类化、屏蔽等)的方法是拥有两个 UIView。两者都是clipToBounds = YES。在子视图上设置圆角,然后将其放置在父视图中,以便裁剪您想要笔直的角。

            UIView* parent = [[UIView alloc] initWithFrame:CGRectMake(10,10,100,100)];
            parent.clipsToBounds = YES;
            
            UIView* child = [[UIView alloc] new];
            child.clipsToBounds = YES;
            child.layer.cornerRadius = 3.0f;
            child.backgroundColor = [UIColor redColor];
            child.frame = CGRectOffset(parent.bounds, +4, -4);
            
            
            [parent addSubView:child];
            

            不支持将两个对角圆角倒圆的情况。

            【讨论】:

              【解决方案14】:

              Bezier 路径是答案,如果您需要其他代码,这对我有用:https://stackoverflow.com/a/13163693/936957

              UIBezierPath *maskPath;
              maskPath = [UIBezierPath bezierPathWithRoundedRect:_backgroundImageView.bounds 
                                               byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) 
                                                     cornerRadii:CGSizeMake(3.0, 3.0)];
              
              CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
              maskLayer.frame = self.bounds;
              maskLayer.path = maskPath.CGPath;
              _backgroundImageView.layer.mask = maskLayer;
              [maskLayer release];
              

              【讨论】:

                【解决方案15】:

                UIBezierPath 解决方案。

                - (void) drawRect:(CGRect)rect {
                
                    [super drawRect:rect];
                
                    //Create shape which we will draw. 
                    CGRect rectangle = CGRectMake(2, 
                                                  2, 
                                                  rect.size.width - 4,
                                                  rect.size.height - 4);
                
                    //Create BezierPath with round corners
                    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rectangle 
                                                                   byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight 
                                                                         cornerRadii:CGSizeMake(10.0, 10.0)];
                
                    //Set path width
                    [maskPath setLineWidth:2];
                
                    //Set color
                    [[UIColor redColor] setStroke];
                
                    //Draw BezierPath to see it
                    [maskPath stroke];
                }
                

                【讨论】:

                  【解决方案16】:

                  这只有在某些东西设置正确的情况下才有效:

                  1. clipsToBounds 必须设置为 YES
                  2. 不透明必须是 NO
                  3. backgroundColor 应该是“clearColor”(我对此不太确定)
                  4. contentMode 必须是“UIContentModeRedraw”,因为如果不是,则不会经常调用 drawRect
                  5. [super drawRect:rect] 必须在 CGContextClip 之后调用
                  6. 您的视图可能不包含任意子视图(对此不确定)
                  7. 请务必至少设置一次“needsDisplay:”以触发您的 drawrect

                  【讨论】:

                    【解决方案17】:

                    我意识到您正在尝试绕过 UITableView 的顶部两个角,但由于某种原因,我发现最好的解决方案是使用:

                    self.tableView.layer.cornerRadius = 10;
                    

                    以编程方式,它应该圆所有四个角,但由于某种原因,它只圆了前两个角。 **请看下面的截图,看看我上面写的代码的效果。

                    我希望这会有所帮助!

                    【讨论】:

                    • 如果它没有将UITableView 的所有角都倒圆角,那么其他地方肯定有代码使UITableView 底部没有圆角,甚至可能是UIView 或@987654326 @ 覆盖UITableView 的底部。
                    【解决方案18】:

                    您可能必须剪辑到边界。添加行

                    self.clipsToBounds = YES
                    

                    在代码中的某处设置该属性。

                    【讨论】:

                      猜你喜欢
                      • 2017-03-25
                      • 2010-12-03
                      • 2018-02-21
                      • 2018-03-24
                      • 2011-05-13
                      • 2013-05-15
                      • 2011-01-24
                      • 2016-01-21
                      • 1970-01-01
                      相关资源
                      最近更新 更多