圆角仅在UIView之上

时间:2012-04-25 13:38:54

标签: objective-c uiview core-graphics rounded-corners

您好我正在搜索一个干净的解决方案而不会覆盖drawRect或类似的东西,以在视图的顶部创建一个带圆角的UIView。我的主要问题是如果视图调整大小或类似的话,创建变量解决方案。 有清洁的解决方案吗? Apple 就是这样做的第一个表项。这样做不会那么难。

10 个答案:

答案 0 :(得分:154)

您可以在视图的图层上设置mask来执行此操作:

CAShapeLayer * maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: self.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii: (CGSize){10.0, 10.}].CGPath;

self.layer.mask = maskLayer;

重要提示:您应该在视图的layoutSubviews()方法中执行此操作,因此视图已从故事板调整大小


在Swift< = 1.2

let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: .TopLeft | .TopRight, cornerRadii: CGSize(width: 10.0, height: 10.0)).CGPath

layer.mask = maskLayer

Swift 2.x

let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: UIRectCorner.TopLeft.union(.TopRight), cornerRadii: CGSizeMake(10, 10)).CGPath
layer.mask = maskLayer

Swift 3.x

let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 10, height: 10)).cgPath
layer.mask = maskLayer

答案 1 :(得分:13)

刚试过Xcode 8.0viewDidLayoutSubviews()

请记住将您的按钮设置为layoutSubViewsyourButton.backgroundColor = UIColor.someColour 为 @rob描述here

当你想改变你的按钮背景时,你只需要打电话:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    yourButton.layer.masksToBounds = true
    yourButton.roundCorners(corners: [.topLeft,.topRight], radius: 5)
}

extension UIButton
{
    func roundCorners(corners:UIRectCorner, radius: CGFloat)
    {
        let maskLayer = CAShapeLayer()
        maskLayer.path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
        self.layer.mask = maskLayer
    }
}

来源:

start1
  • 结果如下:

默认状态:

enter image description here

选择状态:

enter image description here

希望这有帮助!!

答案 2 :(得分:4)

我在阿什利的帮助下解决了这个问题。

首先,我将UIView分类。为我的类创建一个名为- (id)initWithContentView:(UIView *)aView forTableView:(UITableView *)table andIndex:(NSIndexPath *)indexPath;的构造函数。在这个构造函数中,我确定了我想要样式化的表格单元格。

然后我覆盖l - (void)layoutSubviews以创建CAShapeLayer并应用图层蒙版。

.h文件代码

typedef enum {
    tableCellMiddle,
    tableCellTop,
    tableCellBottom,
    tableCellSingle
} tableCellPositionValue;

@interface TableCellBackgrounds : UIView
{
    tableCellPositionValue position;
}

- (id)initWithContentView:(UIView *)aView forTableView:(UITableView *)table andIndex:(NSIndexPath *)indexPath;

@end

.m文件代码

- (id)initWithContentView:(UIView *)aView forTableView:(UITableView *)table andIndex:(NSIndexPath *)indexPath
{
    self = [super initWithFrame:aView.frame];

    [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

    if(self)
    {
        [self setBackgroundColor:[UIColor colorWithRed:(float)230/255 green:(float)80/255 blue:(float)70/255 alpha:1]];

        if(table.style == UITableViewStyleGrouped)
        {
            int rows = [table numberOfRowsInSection:indexPath.section];


            if(indexPath.row == 0 && rows == 1)
            {
                self.layer.cornerRadius = 11;
                position = tableCellSingle;
            }
            else if (indexPath.row == 0)
                position = tableCellTop;
            else if (indexPath.row != rows - 1) 
                position = tableCellMiddle;
            else 
                position = tableCellBottom;
        }
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    if(position == tableCellTop)
    {
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:(CGSize){10.0, 10.0}].CGPath;
        self.layer.mask = maskLayer;
    }
    else if (position == tableCellBottom)
    {
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:(CGSize){10.0, 10.0}].CGPath;
        self.layer.mask = maskLayer;
    } 
}

答案 3 :(得分:3)

UIView的一个扩展,用于对选定的角进行舍入(Swift 4):

extension UIView {

    /// Round UIView selected corners
    ///
    /// - Parameters:
    ///   - corners: selected corners to round
    ///   - radius: round amount
    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}

示例:

ratingView.roundCorners([.topLeft, .topRight, .bottomRight], radius: 6)

答案 4 :(得分:3)

现代便捷的解决方案

iOS 11 +

现在,我们在视图的图层上拥有maskedCorners属性,它使工作变得更加轻松。

只需设置所需的拐角半径并指定应遮盖哪些拐角。最好的部分是,它可以很好地与边框配合使用-图层边框将跟随图层的边缘,无论是否经过圆角处理!在操场上尝试以下代码(请记住按command+option+return来打开实时取景,这样您就可以看到它的外观)

import UIKit
import PlaygroundSupport

let wrapperView = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 160))
wrapperView.backgroundColor = .lightGray

let roundedCornerView = UIView(frame: CGRect(x: 50, y: 50, width: 300, height: 60))
roundedCornerView.backgroundColor = .white

wrapperView.addSubview(roundedCornerView)

roundedCornerView.layer.cornerRadius = 10
roundedCornerView.layer.borderColor = UIColor.red.cgColor
roundedCornerView.layer.borderWidth = 1


// this is the key part - try out different corner combinations to achieve what you need
roundedCornerView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]


PlaygroundPage.current.liveView = wrapperView

是这样的:

enter image description here

答案 5 :(得分:2)

使用swift 3.0,以下为我工作

let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 10, height: 10)).cgPath
(imageView.)layer.mask = maskLayer

重要提示:确保这是'layoutSubviews'而不是'awakeFromNib'(如果你使用的是TableViewCell)或类似的UIView's,或者只有左上角是圆形的!

答案 6 :(得分:2)

对于iOS11及更高版本,您可以使用view属性:

@property CACornerMask maskedCorners

这定义了使用 cornerRadius 属性时四个角中的哪个角接受遮罩。默认为所有四个角。 (Apple文档)

答案 7 :(得分:0)

 CAShapeLayer * maskLayer = [CAShapeLayer layer];
    maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: registerbtn.bounds byRoundingCorners: UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){9.0, 12.0}].CGPath;

    registerbtn.layer.mask = maskLayer;

这只会绕一个角落

答案 8 :(得分:0)

在Objective-C中,它看起来像:

[oCollectionViewCell.layer setMasksToBounds:YES];
[oCollectionViewCell.layer setCornerRadius:5.0];
[oCollectionViewCell.layer setMaskedCorners:kCALayerMinXMinYCorner|kCALayerMaxXMinYCorner];

答案 9 :(得分:-1)

执行此操作的直接方法是在所需的形状中定义路径,并使用您要用于背景的任何颜色填充它。您可以使用UIBezierPathCGPath。例如,使用CGPath,您可以使用CGMoveToPoint()CGAddLineToPoint()CGAddArc()等方法构建路径。然后用CGContextFillPath()填充它。请查看Quartz 2D Programming Guide进行完整讨论。

另一种方法是添加带圆角的子视图(您可以设置子视图的图层的cornerRadius属性),但是让父视图剪切子视图的一侧。

第三种方法是添加具有所需形状的背景图像。您可以使角落透明并使视图的背景透明,您将获得所需的效果。但是,对于调整大小,这不会很好。

你被困在哪里?