【问题标题】:how to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView?如何仅为 UIView 的左下角、右下角和左上角设置cornerRadius?
【发布时间】:2014-10-26 07:19:16
【问题描述】:

有没有办法只为 UIView 的左下角、右下角和左上角设置cornerRadius?

我尝试了以下操作,但最终导致视图消失。下面的代码有什么问题吗?

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

【问题讨论】:

  • 尝试使用UIBezierPath* maskPath = [UIBezierPath new];,然后使用addLineToPoint:addArcWithCenter:radius:startAngle:endAngle:clockwise:方法。
  • 您的代码似乎没问题。只需检查您是否已添加 QuartzCore 框架并在您编写此代码的类中导入。我不知道具体情况,但我之前发生过。

标签: ios objective-c iphone xcode ipad


【解决方案1】:

你可以这样做:

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.viewOutlet.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path  = maskPath.CGPath;
self.viewOutlet.layer.mask = maskLayer;

更新:
如果您需要边框,只需创建另一个CAShapeLayer 并将其添加到视图层作为子层。像这样(把这段代码放在上面的代码下面):

CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
borderLayer.frame = self.view.bounds;
borderLayer.path  = maskPath.CGPath;
borderLayer.lineWidth   = 4.0f;
borderLayer.strokeColor = [UIColor blackColor].CGColor;
borderLayer.fillColor   = [UIColor clearColor].CGColor;

[self.viewOutlet.layer addSublayer:borderLayer];

在这样的 swift 3.0 中:

let maskPath = UIBezierPath.init(roundedRect: self.viewOutlet.bounds, byRoundingCorners:[.topLeft, .bottomLeft], cornerRadii: CGSize.init(width: 10.0, height: 10.0))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.viewOutlet.bounds
maskLayer.path = maskPath.cgPath
self.viewOutlet.layer.mask = maskLayer

【讨论】:

  • 这里我需要为这个视图设置边框...是否可以显示边框?
  • borderLayer.frame = self.view.bounds 表示没有绑定到视图框架。如果动画化了会很难看吗?
  • 这不是很好的工作,例如你有灰色的背景和白色的 UIView。您想要制作带有圆角的形状,该形状将是白色和深灰色的笔触。如果描边小于 4,那么问题就来了
  • 您可能正在使用带有自动调整大小约束的情节提要。试试 this.translatesAutoresizingMaskIntoConstraints = YES;
  • 我使用圆角,但只有左上角和左下角是圆角的。什么给了?
【解决方案2】:

在 xcode 8 和 swift 3 中测试

extension UIView {
    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
    }
}

并像这样使用

YourView.roundCorners([.topLeft, .bottomLeft], radius: 10)

【讨论】:

    【解决方案3】:

    使用自定义UIView 并实现以下代码

    #import "CustomUIView.h"
    
    @implementation CustomView
    
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
        CAShapeLayer * maskLayer = [CAShapeLayer layer];
        maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: self.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){20.0, 20.0}].CGPath;
    
        self.layer.mask = maskLayer;
    }
    
    @end
    

    输出:

    查看UIBezierPath.h 选项:

    typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
        UIRectCornerTopLeft     = 1 << 0,
        UIRectCornerTopRight    = 1 << 1,
        UIRectCornerBottomLeft  = 1 << 2,
        UIRectCornerBottomRight = 1 << 3,
        UIRectCornerAllCorners  = ~0UL
    };
    

    【讨论】:

      【解决方案4】:

      Swift中的视图设置圆角半径

      图层的创建和屏蔽应该在继承自UIView的可选drawRect()块内运行

          override func drawRect(rect: CGRect) {
              super.drawRect(rect)
      
              let maskPath = UIBezierPath(roundedRect: self.view.bounds, byRoundingCorners: [.TopLeft, .BottomLeft, .BottomRight], cornerRadii: CGSizeMake(10, 10))
              let maskLayer = CAShapeLayer()
              maskLayer.frame = self.view.bounds
              maskLayer.path  = maskPath.CGPath
              self.view.layer.mask = maskLayer
          }
      

      【讨论】:

        【解决方案5】:

        我知道已经很晚了,但我只是创建了一种方法,您只能传递一个或多个 UIRectCorners。

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

        【讨论】:

          【解决方案6】:

          希望对你有帮助

          您的上述代码在我的机器上完美运行,可能您将CAShapeLayer 框架设置为等于您的视图框架,因此您的视图将消失,但我在您的代码中没有看到这一行,所以请检查您的视图属性和应用下面的代码

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

          【讨论】:

            【解决方案7】:

            在 iOS 11 上,您只需要 maskedCorners。

                let cornerRadius: CGFloat = 6.0
                if #available(iOS 11, *) {
                    productImageView.layer.cornerRadius  = cornerRadius
                    productImageView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
            
                } else {
                    let path        = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.topLeft , .topRight], cornerRadii:CGSize(width: cornerRadius, height: cornerRadius))
                    let maskLayer   = CAShapeLayer()
                    maskLayer.frame = bounds
                    maskLayer.path  = path.cgPath
                    productImageView.mask                = maskLayer
                    productImageView.layer.masksToBounds = true
                }
            

            【讨论】:

              【解决方案8】:
              UIBezierPath *maskPath;
              maskPath = [UIBezierPath bezierPathWithRoundedRect:someView.bounds 
                                           byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight) 
                                                 cornerRadii:CGSizeMake(10.0,10.0,5.0, 10.0)];
              
              
              CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
              maskLayer.frame = self.bounds;
              maskLayer.path = maskPath.CGPath;
              someView.layer.mask = maskLayer;
              

              【讨论】:

                【解决方案9】:

                试试这个代码

                -(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;
                }
                

                调用使用

                [self setMaskTo:myView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
                

                可用选项:

                UIRectCornerTopLeft, UIRectCornerTopRight,UIRectCornerBottomLeft,UIRectCornerBottomRight,UIRectCornerAllCorners
                

                【讨论】:

                  【解决方案10】:

                  仅使用此代码的两个边角半径。很长一段时间后,我得到了建议 ios。这里 viewFilter 是我们想要四舍五入的视图出口。

                  -(void) viewDidAppear:(BOOL)animated{
                  [super viewDidAppear:animated];
                  [self setMaskTo:viewFilter byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
                  }
                  
                  -(void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners{
                  
                  UIBezierPath *rounded = [UIBezierPathbezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(20.0, 20.0)];
                  
                  CAShapeLayer *shape = [[CAShapeLayer alloc] init];
                  shape.frame = self.view.bounds;
                  [shape setPath:rounded.CGPath];
                  view.layer.mask = shape;
                  }
                  

                  【讨论】:

                    猜你喜欢
                    • 2015-09-22
                    • 2012-04-27
                    • 1970-01-01
                    • 2013-12-22
                    • 2021-08-21
                    • 1970-01-01
                    • 2020-05-07
                    • 2016-06-08
                    相关资源
                    最近更新 更多