Swift:带内部阴影的圆形视图

Swift:带内部阴影的圆形视图,swift,uiview,border,Swift,Uiview,Border,我正在这里尝试实施解决方案(我将其转换为Swift): 我的代码如下: mobileInputView.layer.cornerRadius = 5.0; let shadowLayer = CAShapeLayer() shadowLayer.frame = mobileInputView.bounds; // Standard shadow stuff shadowLayer.shadowColor = UIColor(white: 0, alph

我正在这里尝试实施解决方案(我将其转换为Swift):

我的代码如下:

    mobileInputView.layer.cornerRadius = 5.0;

    let shadowLayer = CAShapeLayer()
    shadowLayer.frame = mobileInputView.bounds;

    // Standard shadow stuff
    shadowLayer.shadowColor = UIColor(white: 0, alpha: 1).CGColor
    shadowLayer.shadowOffset = CGSizeMake(0.0, 0.0)
    shadowLayer.shadowOpacity = 1.0
    shadowLayer.shadowRadius = 4

    // Causes the inner region in this example to NOT be filled.
    shadowLayer.fillRule = kCAFillRuleEvenOdd

    // Create the larger rectangle path.
    let path = CGPathCreateMutable();

    CGPathAddRect(path, nil, CGRectInset(mobileInputView.bounds, -42, -42));

    // Add the inner path so it's subtracted from the outer path.
    // someInnerPath could be a simple bounds rect, or maybe
    // a rounded one for some extra fanciness.
    let someInnerPath = UIBezierPath(roundedRect: mobileInputView.bounds, cornerRadius:5.0).CGPath
    CGPathAddPath(path, nil, someInnerPath)
    CGPathCloseSubpath(path);

    shadowLayer.path = path

    //CGPathRelease(path);

    mobileInputView.layer.addSublayer(shadowLayer)
    //[[_textField layer] addSublayer:shadowLayer];

    let maskLayer = CAShapeLayer()
    maskLayer.path = someInnerPath
    shadowLayer.mask = maskLayer
但最终的结果是:

组件的宽度是正确的,我可以一直打到屏幕的边缘,只是边框的大小不好。如果我设置视图的背景色,我可以看到视图本身的大小正确


如果我尝试使用.frame而不是.bounds,我会得到相同的结果。

实现这一点的更简单的方法是直接修改UITextField,而不使用大纲UIView和标签:

    let label = UILabel(frame: CGRectMake(1, 1, 125, 25))
    label.text = "Mobile number:"
    label.textAlignment = NSTextAlignment.Right

    mobileNumber.leftView = label
    mobileNumber.leftViewMode = UITextFieldViewMode.Always

    mobileNumber.borderStyle = UITextBorderStyle.Bezel;
    mobileNumber.layer.borderWidth = 1.0
    mobileNumber.layer.borderColor = UIColor.grayColor().CGColor
    mobileNumber.layer.cornerRadius = 5.0