简体   繁体   中英

swift - UIView shadow not rendering properly

I have UIView inside my cell. UICollectionview first loads, shadows are looking not as I intended. But when I scroll, shadows become normal.

        self.backView.layer.cornerRadius = 3
        self.backView.clipsToBounds = true
        self.backView.layer.shadowPath = UIBezierPath(roundedRect: self.backView.bounds, cornerRadius: self.backView.layer.cornerRadius).CGPath
        self.backView.layer.shadowColor = UIColor.blackColor().CGColor
        self.backView.layer.shadowOpacity = 0.3
        self.backView.layer.shadowOffset = CGSizeMake(0, 1)
        self.backView.layer.shadowRadius = 1
        self.backView.layer.masksToBounds = false

Shadows rendered wrong:

错误

Shadows rendered correctly:

在此处输入图片说明

Edit-1 : I found the root cause but not the solution. Cell frame of the storyboard and cell frame of the run time are different because I change the cell height&width like this:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(collectionView.frame.width/3, collectionView.frame.height)
    }

So when I remove this code, shadows are displayed properly. But I can't set width&height via code.

you can try this

self.backView.layer.shadowColor = UIColor.black.cgColor
self.backView.layer.shadowOpacity = 1
self.backView.layer.shadowOffset = CGSize.zero
self.backView.layer.shadowRadius = 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM