我的登录视图有一个子视图,它有一个UIActivityView和一个UILabel,上面写着“Signing In…”这个子视图有不圆角的角。我怎样才能把它们变圆呢?

有什么办法在我的xib里面做吗?


当前回答

在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
    }
}

其他回答

请导入Quartzcore框架,然后你必须设置setMaskToBounds为TRUE,这是非常重要的一行。

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

斯威夫特

简短的回答:

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属性来给它圆角。

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

结果是这样的:

clipsToBounds和masksToBounds都是等价的。只是第一个用于UIView,第二个用于CALayer。

另请参阅

如何添加边界和阴影 贝塞尔曲线路径 转换

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];

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