如何制作带有可选圆角和边框的 UIView? [英] How to make a UIView with optional rounded corners and border?

查看:25
本文介绍了如何制作带有可选圆角和边框的 UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将圆角半径应用于 UIView,即 UIRectCornerTopLeftUIRectCornerTopRight.当我应用它时,边界在角落处消失了.如何避免这种情况?

I am applying corner radius to a UIView i.e. UIRectCornerTopLeft and UIRectCornerTopRight. When I apply this, the border is gone at the corners. How to avoid this?

这就是我将边框应用于 UIView 的方式:

This is how I apply border to UIView:

 [self.middleView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(4, 4)];
 self.middleView.layer.borderWidth = 0.5f;
 self.middleView.layer.borderColor = [[UIColor colorWith8BitRed:0 green:0 blue:0 alpha:0.25]

这是我用于应用可选圆角的类别:

And this is a category I am using for applying optional rounded corners:

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

推荐答案

试试下面的代码就行了

Try below code it work

您想要四舍五入 TopLeft 和 TopRight 的视图

   UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)];
   [view1 setBackgroundColor:[UIColor grayColor]];
   [self.view addSubview:view1];

设置角落如下代码

UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:view1.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(5.0, 5.0)];

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

输出是:

这篇关于如何制作带有可选圆角和边框的 UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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