2

I want to draw shadow around MyCustomView which is created in this way:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let header = MyCustomView() header.contentView.dropShadow() // tried also header.dropShadow() return header } 

And here are definitions:

class MyCustomView: UIView { //... var contentView = UIView() init() { super.init(frame: CGRect( x: CGFloat(0), y: CGFloat(0), width: CGFloat(200), height: CGFloat(200))) //... let marginGuide = self.layoutMarginsGuide contentView.addSubview(someLabel) contentView.addSubview(anotherLabel) // Background colors self.backgroundColor = UIColor.init(hex: "EBEBF1") contentView.backgroundColor = .white // translatesAutoresizingMaskIntoConstraints contentView.translatesAutoresizingMaskIntoConstraints = false // contentView constraints contentView.topAnchor.constraint( equalTo: marginGuide.topAnchor, constant: CGFloat(0)).isActive = true contentView.bottomAnchor.constraint( equalTo: marginGuide.bottomAnchor, constant: marginSize).isActive = true contentView.trailingAnchor.constraint( equalTo: marginGuide.trailingAnchor, constant: -marginSize).isActive = true contentView.leadingAnchor.constraint( equalTo: marginGuide.leadingAnchor, constant: CGFloat(7)).isActive = true } } extension UIView { func dropShadow(scale: Bool = true) { self.layer.masksToBounds = false self.layer.shadowColor = UIColor.darkGray.cgColor self.layer.shadowOpacity = 0.5 self.layer.shadowOffset = CGSize(width: 0, height: -1) self.layer.shadowRadius = 3 self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath self.layer.shouldRasterize = true self.layer.rasterizationScale = scale ? UIScreen.main.scale : 1 } } 

Instead of shadow I get warnings:

<CATransformLayer: 0x60400023e780> - changing property shadowColor in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shadowOpacity in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shadowOffset in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shadowPath in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shouldRasterize in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property rasterizationScale in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shadowPath in transform-only layer, will have no effect

Do you have any idea why is this happening?

4
  • Add the rest of the code of MyCustomView - specifically the code of laying out contentView Commented Jan 4, 2018 at 20:16
  • your extension is for the UIView class Commented Jan 4, 2018 at 20:21
  • @MilanNosáľ - added. Commented Jan 4, 2018 at 20:24
  • @ReinierMelian - that's right, and contentView is also UIView Commented Jan 4, 2018 at 20:24

1 Answer 1

2

Try calling your dropShadow() function in layoutSubviews method of your MyCustomView:

class MyCustomView: UIView { //.. your implementation override func layoutSubviews() { super.layoutSubviews() contentView.dropShadow() } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.