如何在iOS中给像卡一样的影子

时间:2016-04-06 08:11:41

标签: ios objective-c uitableview

我想像我的iOS应用程序中的图像一样给出类似卡片的阴影效果

enter image description here

我在UITableViewCell上需要这个图像对我来说也不适用于带有阴影效果的单元格之间的间隙

5 个答案:

答案 0 :(得分:13)

在表格视图单元格中使用容器视图并指定标记99。 保持单元格高度比卡(容器视图)大一点。

并为您的卡片视图添加阴影

UIView* shadowView = [cell viewWithTag:99];
shadowView.backgroundColor=[UIColor colorWithRed:228.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:0.5];
[shadowView.layer setCornerRadius:5.0f];
[shadowView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[shadowView.layer setBorderWidth:0.2f];
[shadowView.layer setShadowColor:[UIColor colorWithRed:225.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:1.0].CGColor];
[shadowView.layer setShadowOpacity:1.0];
[shadowView.layer setShadowRadius:5.0];
[shadowView.layer setShadowOffset:CGSizeMake(5.0f, 5.0f)];

答案 1 :(得分:8)

swift 3.0上的即兴解决方案:

extension UIView {

func setCardView(view : UIView){

    view.layer.cornerRadius = 5.0
    view.layer.borderColor  =  UIColor.clear.cgColor
    view.layer.borderWidth = 5.0
    view.layer.shadowOpacity = 0.5
    view.layer.shadowColor =  UIColor.lightGray.cgColor
    view.layer.shadowRadius = 5.0
    view.layer.shadowOffset = CGSize(width:5, height: 5)
    view.layer.masksToBounds = true

}
}

<强>用法:

on cellForRowAt indexPath:

var cell = UITableViewCell()

cell.contentView.setCardView(view: cell.contentView)

答案 2 :(得分:1)

也许有人需要Swift版

func setCardView(view : UIView){

        view.layer.masksToBounds = false
        view.layer.shadowOffset = CGSizeMake(0, 0);
        view.layer.cornerRadius = 1; 
        view.layer.shadowRadius = 1;
        view.layer.shadowOpacity = 0.5;

    }

答案 3 :(得分:1)

您可以使用此代码提供阴影效果......

UIView *viewTemp= (UIView *)view;
viewTemp.layer.shadowColor = [UIColor darkGrayColor].CGColor;
viewTemp.layer.shadowOffset = CGSizeMake(0, 2);
viewTemp.layer.shadowOpacity = 0.8;
viewTemp.layer.shadowRadius = 3;
viewTemp.layer.masksToBounds = NO;

答案 4 :(得分:0)

    extension UIView {


          func addShadow(){
            self.layer.cornerRadius = 20.0
            self.layer.shadowColor = UIColor.gray.cgColor
            self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
            self.layer.shadowRadius = 12.0
            self.layer.shadowOpacity = 0.7

         }  
}