首页 文章

给UIView圆角

提问于
浏览
534

我的登录视图有一个子视图,其中包含 UIActivityViewUILabel ,表示"Signing In…" . 此子视图的拐角不是圆角 . 我怎样才能让它们成圆形?

有没有办法在我的xib中做到这一点?

19 回答

  • -4

    试试这个

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

    ...

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

    注意:如果您尝试将圆角应用于 UIViewController 's view, it should not be applied in the view controller'构造函数,而是在 -viewDidLoad 中,则实际实例化 view 之后 .

  • 39

    如果圆角在 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);
    }
    

    希望这可以帮助!

  • 3

    你需要先导入头文件 <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 ,否则可能无法显示效果 .

  • 43

    斯威夫特

    简短回答:

    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)
    

    您可以通过更改视图的 layercornerRadius 属性为其提供圆角 .

    blueView.layer.cornerRadius = 8
    

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

    blueView.layer.cornerRadius = 25
    

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

    blueView.layer.cornerRadius = 3
    

    这可能足以解决您的问题 . 但是,有时视图可以具有超出视图边界的子视图或子图层 . 例如,如果我要像这样添加一个子 view

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

    或者如果我要像这样添加一个子 layer

    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 都是equivalent . 只是第一个与 UIView 一起使用,第二个与 CALayer 一起使用 .

    另见

  • 0

    为圆视图设置cornerRadious属性

    set masksToBounds布尔值的图像将不会在角半径边界外绘制

    view.layer.cornerRadius = 5;
    
    view.layer.masksToBounds = YES;
    
  • 26

    Swift 4 - 使用IBDesignable

    @IBDesignable
        class DesignableView: UIView {
        }
    
        extension UIView
        {
    
            @IBInspectable
            var cornerRadius: CGFloat {
                get {
                    return layer.cornerRadius
                }
                set {
                layer.cornerRadius = newValue
            }
        }
    }
    
  • 1168

    在对象中以编程方式执行此操作

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

    我们也可以从stoaryboard这样做 .

    layer.cornerRadius  Number  5
    
  • 2

    在Xcode 6上试试吧

    self.layer.layer.cornerRadius = 5.0f;
    

    要么

    self.layer.layer.cornerRadius = 5.0f;
        self.layer.clipsToBounds = YES;
    
  • 9
    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];
    
  • 1
    view.layer.cornerRadius = 25
    view.layer.masksToBounds = true
    
  • 58

    this blog post中所述,这是一个圆角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];
    }
    

    关于它的很酷的部分是你可以选择你想要四舍五入的角落 .

  • 2

    您还可以使用图片:

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

    您还可以使用界面构建器的 User Defined Runtime Attributes 功能将键路径 layer.cornerRadius 设置为值 . 请确保包含 QuartzCore 库 .

    这个技巧也适用于设置layer.borderWidth但是它不适用于 layer.borderColor ,因为这需要 CGColor 而不是 UIColor .

    您将无法在故事板中看到效果,因为这些参数是在运行时评估的 .

    Using Interface builder to set the corner radius

  • 1

    您可以使用以下自定义 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
            }
        }
    
    }
    
  • 0

    请导入 Quartzcore framework 然后你必须将 setMaskToBounds 设置为 TRUE 这是非常重要的一行 .

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

  • 6
    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;
    }
    
  • 250

    使用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)
    
    }
    
  • 19

    与Ed Marty所做的不同:

    #import <QuartzCore/QuartzCore.h>
    
    [v.layer setCornerRadius:25.0f];
    [v.layer setMasksToBounds:YES];
    

    你需要setMasksToBounds才能从IB加载所有对象...我遇到了一个问题,我的视图已经四舍五入,但是没有来自IB的对象:/

    这固定了= D希望它有所帮助!

  • 2

    现在,您可以在UIView中使用swift类别(图片中的代码),并使用@IBInspectable在故事板上显示结果(如果您使用的是类别,请仅使用cornerRadius而不是layer.cornerRadius作为键路径 .

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

    enter image description here

相关问题