设置阴影效果
阴影效果无非跟layer层相关,我们只需对layer进行操作即可
其属性包括:shadowOffset
、 shadowOpacity
、 shadowColor
,
有圆角的情况下,还需要加上 cornerRadius
和 clipsToBounds
属性,
1 2 3 4 5 6 7 8 9 10
| UIButton * divTaoBtn = [UIButton buttonWithType:UIButtonTypeCustom]; divTaoBtn.frame = CGRectMake(100, 100, 100, 40); [divTaoBtn setTitle:@"DivTao" forState:UIControlStateNormal]; // 标题颜色默认是 whiteColor [divTaoBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [divTaoBtn setBackgroundColor:[UIColor whiteColor]]; // 设置背景颜色,目的显示边框 divTaoBtn.layer.cornerRadius = 4; // 切圆角 divTaoBtn.layer.shadowOffset = CGSizeMake(1, 1); // 阴影偏移 divTaoBtn.layer.shadowOpacity = 0.8; // 设置透明度(也可不加) divTaoBtn.layer.shadowColor = [UIColor grayColor].CGColor; // 设置阴影颜色 [self.view addSubview:divTaoBtn];
|
参考资料
iOS下按钮同时实现圆角与阴影效果
UITableViewCell上加阴影效果
因项目需要,需要给tableview cell添加阴影:
1 2 3 4 5 6 7
| cell.layer.shadowOffset = CGSizeMake(0, 1); cell.layer.shadowColor = [UIColor grayColor].CGColor; cell.layer.shadowRadius = 1; cell.layer.shadowOpacity = .5f; CGRect shadowFrame = cell.layer.bounds; CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath; cell.layer.shadowPath = shadowPath;
|
注意:不要给section footer 或 section header设置背景色,否则无效果
参考资料
为tableView添加阴影效果