UIView阴影和圆角

时间:2014-07-24 07:31:42

标签: ios objective-c uiview calayer shadow

我有一个UIView,我希望有一个阴影和圆角。 问题是 - 阴影需要masksToBounds = NO而圆角需要masksToBounds = YES

我找到的解决方案是有一个容器,它将有阴影并将我的UIView添加为容器的子视图 - 并给它圆角。

这很有效。我有阴影和圆角 - 但它没有好处。 阴影是一个矩形视图,我的图像有圆角。

Shadow and rounded corners

如何为圆角实现阴影?

3 个答案:

答案 0 :(得分:1)

import UIKit

@IBDesignable
class customButton: UIView {

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

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

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


}

将此设置为UIView的类,然后放置maskToBound = true。然后在检查器中相应地设置该值以得到一个四舍五入阴影。

答案 1 :(得分:0)

删除容器,剪切视图阴影。导入QuartzCore/QuartzCore.h。并尝试将此代码添加到您的视图中

#import < QuartzCore/QuartzCore.h>

...

view.layer.cornerRadius = 5.0f;
[view.layer setShadowColor:[UIColor redColor].CGColor];
[view.layer setShadowOpacity:0.7];
[view.layer setShadowRadius:5.0];
[view.layer setShadowOffset:CGSizeMake(2.0, 2.0)];

了解有关maskToBound的更多信息,请参阅此链接

What UIView layer.masksToBounds is doing if set to YES?

答案 2 :(得分:-1)

// border radius
[yourView.layer setCornerRadius:30.0f];

// border
[yourView.layer setBorderColor:[UIColor blackColor].CGColor];
[yourView.layer setBorderWidth:1.5f];
 yourView.layer.masksToBounds=YES;

// drop shadow
[yourView.layer setShadowColor:[UIColor lightGrayColor].CGColor];
[yourView.layer setShadowOpacity:0.8];
[yourView.layer setShadowRadius:3.0];
[yourView.layer setShadowOffset:CGSizeMake(2.0, 2.0)];

这可能会对你有帮助。

相关问题