597

我的登录视图有一个子视图,其中有一个UIActivityView和一个UILabel说法“登录...”。此子视图具有未圆角的角。我怎样才能让它们变圆?

有什么办法可以在我的 xib 中做到这一点?

4

21 回答 21

1252

试试这个

#import <QuartzCore/QuartzCore.h> // not necessary for 10 years now  :)

...

view.layer.cornerRadius = 5;
view.layer.masksToBounds = true;

注意:如果您尝试将圆角应用于 aUIViewController的视图,则不应将其应用于视图控制器的构造函数,而应将其应用于实际实例化的-viewDidLoad, after 。view

于 2009-10-02T13:40:44.790 回答
268

您还可以使用界面构建器的用户定义的运行时属性功能将键路径设置layer.cornerRadius为值。确保你包括QuartzCore图书馆。

这个技巧也适用于设置 layer.borderWidth 但是它不会起作用,layer.borderColor因为这需要 a CGColornot a UIColor

您将无法在情节提要中看到效果,因为这些参数是在运行时评估的。

使用界面生成器设置拐角半径

于 2012-07-15T22:17:11.987 回答
69

迅速

简短的回答:

myView.layer.cornerRadius = 8
myView.layer.masksToBounds = true  // optional

补充答案

如果您得到了这个答案,那么您可能已经看到了足够的内容来解决您的问题。我添加这个答案是为了更直观地解释为什么事情会做他们所做的事情。

如果你从常规开始,UIView它有方角。

let blueView = UIView()
blueView.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
blueView.backgroundColor = UIColor.blueColor()
view.addSubview(blueView)

在此处输入图像描述

cornerRadius您可以通过更改视图的属性来给它圆角layer

blueView.layer.cornerRadius = 8

在此处输入图像描述

较大的半径值会产生更多的圆角

blueView.layer.cornerRadius = 25

在此处输入图像描述

较小的值会产生较少的圆角。

blueView.layer.cornerRadius = 3

在此处输入图像描述

这可能足以解决您的问题。然而,有时一个视图可能有一个超出视图边界的子视图或子层。例如,如果我要添加这样的子视图

let mySubView = UIView()
mySubView.frame = CGRect(x: 20, y: 20, width: 100, height: 100)
mySubView.backgroundColor = UIColor.redColor()
blueView.addSubview(mySubView)

或者如果我要添加这样的子

let mySubLayer = CALayer()
mySubLayer.frame = CGRect(x: 20, y: 20, width: 100, height: 100)
mySubLayer.backgroundColor = UIColor.redColor().CGColor
blueView.layer.addSublayer(mySubLayer)

然后我会结束

在此处输入图像描述

现在,如果我不想让事情超出界限,我可以这样做

blueView.clipsToBounds = true

或这个

blueView.layer.masksToBounds = true

这给出了这个结果:

在此处输入图像描述

两者clipsToBoundsmasksToBounds等价的。只是第一个用于 with UIView,第二个用于 with CALayer

也可以看看

于 2016-01-25T01:07:44.273 回答
66

现在您可以在 UIView 中使用 swift 类别(图片下方的代码)和 @IBInspectable 以在情节提要中显示结果(如果您使用该类别,请仅使用cornerRadius 而不是 layer.cornerRadius 作为关键路径。

extension UIView {
    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }
}

在此处输入图像描述

于 2014-01-16T16:26:42.147 回答
44

与 Ed Marty 所做的不同的方法:

#import <QuartzCore/QuartzCore.h>

[v.layer setCornerRadius:25.0f];
[v.layer setMasksToBounds:YES];

您需要 setMasksToBounds 才能从 IB 加载所有对象...我遇到了一个问题,我的视图变圆了,但没有来自 IB 的对象:/

这修复了它=D希望它有帮助!

于 2010-12-09T12:23:36.337 回答
28

这篇博文中所述,这是一种对 UIView 进行圆角处理的方法:

+(void)roundView:(UIView *)view onCorner:(UIRectCorner)rectCorner radius:(float)radius
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                   byRoundingCorners:rectCorner
                                                         cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    [view.layer setMask:maskLayer];
    [maskLayer release];
}

它最酷的部分是您可以选择要四舍五入的角。

于 2012-09-10T11:09:01.150 回答
20

你需要先导入头文件<QuartzCore/QuartzCore.h>

 #import QuartzCore/QuartzCore.h>

[yourView.layer setCornerRadius:8.0f];
yourView.layer.borderColor = [UIColor redColor].CGColor;
yourView.layer.borderWidth = 2.0f;
[yourView.layer setMasksToBounds:YES];

不要错过使用 - setMasksToBounds,否则可能无法显示效果。

于 2012-08-01T10:19:20.560 回答
19

您可以使用以下自定义UIView类,它也可以更改边框颜色和宽度。因为这是IBDesignalbe您也可以更改界面生成器中的属性。

在此处输入图像描述

import UIKit

@IBDesignable public class RoundedView: UIView {

    @IBInspectable var borderColor: UIColor = UIColor.white {
        didSet {
            layer.borderColor = borderColor.cgColor
        }
    }

    @IBInspectable var borderWidth: CGFloat = 2.0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }

    @IBInspectable var cornerRadius: CGFloat = 0.0 {
        didSet {
            layer.cornerRadius = cornerRadius
        }
    }

}
于 2016-10-26T08:31:46.020 回答
6
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 50, 200, 200)];

view.layer.backgroundColor = [UIColor whiteColor].CGColor;
view.layer.cornerRadius = 20.0;
view.layer.frame = CGRectInset(v.layer.frame, 20, 20);

view.layer.shadowOffset = CGSizeMake(1, 0);
view.layer.shadowColor = [[UIColor blackColor] CGColor];
view.layer.shadowRadius = 5;
view.layer.shadowOpacity = .25;

[self.view addSubview:view];
[view release];
于 2012-05-23T11:51:10.450 回答
5
view.layer.cornerRadius = 25
view.layer.masksToBounds = true
于 2018-04-16T12:10:10.187 回答
4

如果圆角在viewDidload()中不起作用,最好在viewDidLayoutSubview()中编写代码

-(void)viewDidLayoutSubviews
{
    viewTextfield.layer.cornerRadius = 10.0 ;                                               
    viewTextfield.layer.borderWidth = 1.0f;
    viewTextfield.layer.masksToBounds =  YES;
    viewTextfield.layer.shadowRadius = 5;
    viewTextfield.layer.shadowOpacity = 0.3;
    viewTextfield.clipsToBounds = NO;
    viewTextfield.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}

希望这可以帮助!

于 2018-04-21T06:22:59.530 回答
4

- 斯威夫特用户界面

在 SwiftUI 中,你可以cornerRadius直接在任何View你想要的地方使用修饰符。例如这个问题:

Text("Signing In…")
    .padding(16)
    .background(Color.red)
    .cornerRadius(50)

预览

请注意,没有更多的菱形半径,因此即使您将cornerRadius设置为超过高度的一半,它也会平滑圆润。

查看 此答案以了解如何在 SwiftUI 中转角

于 2019-10-09T19:49:03.693 回答
3

Swift 4 - 使用 IBDesignable

   @IBDesignable
    class DesignableView: UIView {
    }

    extension UIView
    {

        @IBInspectable
        var cornerRadius: CGFloat {
            get {
                return layer.cornerRadius
            }
            set {
            layer.cornerRadius = newValue
        }
    }
}
于 2017-10-24T04:34:09.107 回答
2

请导入Quartzcore framework,然后您必须将此设置setMaskToBoundsTRUE非常重要的行。

然后:[[yourView layer] setCornerRadius:5.0f];

于 2013-02-16T09:52:18.100 回答
2
UIView* viewWithRoundedCornersSize(float cornerRadius,UIView * original)
{
    // Create a white border with defined width
    original.layer.borderColor = [UIColor yellowColor].CGColor;
    original.layer.borderWidth = 1.5;

    // Set image corner radius
    original.layer.cornerRadius =cornerRadius;

    // To enable corners to be "clipped"
    [original setClipsToBounds:YES];
    return original;
}
于 2014-09-02T07:48:49.817 回答
2

在 obj c 中以编程方式执行此操作

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 50,    200, 200)];

view.layer.backgroundColor = [UIColor whiteColor].CGColor;
view.layer.cornerRadius = 20.0;
view.layer.frame = CGRectInset(v.layer.frame, 20, 20);

[view.layer.shadowOffset = CGSizeMake(1, 0);
view.layer.shadowColor = [[UIColor blackColor] CGColor];
view.layer.shadowRadius = 5;
view.layer.shadowOpacity = .25;][1]

[self.view addSubview:view];

我们也可以从故事板上做到这一点。

 layer.cornerRadius  Number  5

在此处输入图像描述

于 2017-02-09T09:19:55.160 回答
2

在 Swift 4.2 和 Xcode 10.1 中

let myView = UIView()
myView.frame = CGRect(x: 200, y: 200, width: 200, height: 200)
myView.myViewCorners()
//myView.myViewCorners(width: myView.frame.width)//Pass View width
view.addSubview(myView)

extension UIView {
    //If you want only round corners
    func myViewCorners() {
        layer.cornerRadius = 10
        layer.borderWidth = 1.0
        layer.borderColor = UIColor.red.cgColor
        layer.masksToBounds = true
    }
    //If you want complete round shape, enable above comment line
    func myViewCorners(width:CGFloat) {
        layer.cornerRadius = width/2
        layer.borderWidth = 1.0
        layer.borderColor = UIColor.red.cgColor
        layer.masksToBounds = true
    }
}
于 2019-02-01T09:59:46.517 回答
0

您还可以使用图像:

UIImage *maskingImage = [UIImage imageNamed:@"bannerBarBottomMask.png"];
CALayer *maskingLayer = [CALayer layer];
maskingLayer.frame = CGRectMake(-(self.yourView.frame.size.width - self.yourView.frame.size.width) / 2
                                , 0
                                , maskingImage.size.width
                                , maskingImage.size.height);
[maskingLayer setContents:(id)[maskingImage CGImage]];
[self.yourView.layer setMask:maskingLayer];
于 2013-01-24T06:59:09.700 回答
0

为圆形视图设置cornerRadious属性

为图像设置 maskToBounds 布尔值仍不会在圆角半径边界之外绘制

view.layer.cornerRadius = 5;

view.layer.masksToBounds = YES;
于 2014-12-29T11:29:31.113 回答
0

使用 UIView 扩展:

extension UIView {    

func addRoundedCornerToView(targetView : UIView?)
{
    //UIView Corner Radius
    targetView!.layer.cornerRadius = 5.0;
    targetView!.layer.masksToBounds = true

    //UIView Set up boarder
    targetView!.layer.borderColor = UIColor.yellowColor().CGColor;
    targetView!.layer.borderWidth = 3.0;

    //UIView Drop shadow
    targetView!.layer.shadowColor = UIColor.darkGrayColor().CGColor;
    targetView!.layer.shadowOffset = CGSizeMake(2.0, 2.0)
    targetView!.layer.shadowOpacity = 1.0
}
}

用法:

override func viewWillAppear(animated: Bool) {

sampleView.addRoundedCornerToView(statusBarView)

}
于 2016-05-11T06:24:32.237 回答
-4

ON Xcode 6 你的尝试

     self.layer.layer.cornerRadius = 5.0f;

或者

    self.layer.layer.cornerRadius = 5.0f;
    self.layer.clipsToBounds = YES;
于 2014-11-28T12:42:37.310 回答