The shadow not appear in collectionViewcell - swift

I create a collectionView and I want to add shadow to every cell, I wrote this code
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MyCollectionViewCell
cell.myLabel.text = self.items[indexPath.item]
cell.backgroundColor = UIColor.yellow
cell.layer.shadowColor = UIColor.black.cgColor
cell.layer.shadowOffset = CGSize(width: 3, height: 3)
cell.layer.shadowOpacity = 0.7
cell.layer.shadowRadius = 4.0
return cell
}
but this is the output
I want it be like this
UIView with rounded corners and drop shadow?

Your code will probably work if you also say:
cell.layer.masksToBounds = false
The reason is that a shadow is outside the layer's bounds, so if we are masking to the bounds, you won't see the shadow.

Related

CollectionViewCell drop shadow with rounded corners

I've collectionViewCell and want to drop shadow to it, also want to have rounded corners, but it seems impossibile, cause to drop shadow clipToBounds must be set to false, with that, I can't add rounded corners, is there any kind of a "hack" to achieve my target?
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let countryCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MySubscribitionsCollectionViewCell
countryCell.layer.cornerRadius = 10
countryCell.layer.borderWidth = 1.0
countryCell.layer.borderColor = UIColor.lightGray.cgColor
countryCell.layer.backgroundColor = UIColor.white.cgColor
countryCell.layer.shadowColor = UIColor.black.cgColor
countryCell.layer.shadowOffset = CGSize(width: 2.0, height: 4.0)
countryCell.layer.shadowRadius = 10.0
countryCell.layer.shadowOpacity = 0.5
countryCell.clipsToBounds = false
}
You can use another view to cast the shadow
Something like that
let shadowView = UIView()
shadowView.layer.masksToBounds = false
// SET SHADOW
let mainView = UIView()
mainView.layer.cornerRadius = 10
mainView.layer.masksToBounds = true
shadowView.addSubview(mainView)
In the case of a cell, you can set the shadow on the cell itself, and set the rounding of the corners and your custom views on the contentView of collectionViewCell

How to avoid resizing UITextView content when applying a transform

I'm making a little notes app in which i've added a textView inside each cell, when selecting a cell it scales itself to certain size so the textView is scaled too. Everything is fine except that the text inside the textView is also scaled and looks pretty bad.
Does anyone know how to prevent the text from resizing or how can i handle this issue?
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseIdentifier, for: indexPath) as! collectionViewCell3
cell.clipsToBounds = false
cell.layer.cornerRadius = 10
noteTextView[indexPath.item].frame = CGRect(x: 10, y: 10, width: cell.frame.width - 20, height: cell.frame.height - 20)
noteTextView[indexPath.item].delegate = self
noteTextView[indexPath.item].textContainer.maximumNumberOfLines = 10
noteTextView[indexPath.item].isScrollEnabled = false
noteTextView[indexPath.item].font = UIFont(name: "Noteworthy-Light", size: 12)
noteTextView[indexPath.item].text = items[indexPath.item]
cell.contentView.addSubview(noteTextView[indexPath.item])
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
index = indexPath
notasCollectionView.isScrollEnabled = false
let cell = notasCollectionView.cellForItem(at: indexPath)
let desiredPointY = notasCollectionView.contentOffset.y + 350
let desiredPointX = notasCollectionView.center.x
let differenceX = desiredPointX - cell!.frame.midX
let differenceY = desiredPointY - cell!.frame.mid
let differenceHeight = 354 / (cell?.frame.height)!
var tt = CGAffineTransform(scaleX: 1.89304813, y: differenceHeight)
tt = tt.concatenating(CGAffineTransform(translationX: differenceX, y: differenceY))
UIView.animate(withDuration: 0.2, delay: 0.0, animations: {
self.notasCollectionView.bringSubviewToFront(cell!)
cell!.transform = tt
}, completion: { _ in
UIView.animate(withDuration: 0.2) {
cell?.layer.cornerRadius = 0
}
})
}
Here are the screenshots:
[![Screenshot 1][1]][1] [![Screenshot 2][2]][2]
[1]: https://i.stack.imgur.com/cYURo.png [2]: https://i.stack.imgur.com/LbDND.jpg
The problem is you are increasing the size of the text view but the font size is the same.
So you need to change the font size according to your scaling multiplier.

TableView Cell view shadow does not coming properly in Swift

I have added view as a background for tableview cell and I am giving shadow for view. Here when I run tableview shadow does not coming properly, once I scroll down and up then shadow coming properly.
I have given shadow according to this answer answer
Code:
extension UIView {
func dropShadow(scale: Bool = true) {
layer.masksToBounds = false
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.5
layer.shadowOffset = CGSize(width: -1, height: 1)
layer.shadowRadius = 1
layer.shadowPath = UIBezierPath(rect: bounds).cgPath
layer.shouldRasterize = true
layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PlansTableViewCell
cell.containerView.dropShadow()
return cell
}
Before scrolling shadow coming like this:
after scrolling coming like below:
After running(before scrolling) also i need second image kind of output, Here Help me with the code.
Problem is here
layer.shadowPath = UIBezierPath(rect: bounds).cgPath
bounds isn't correct at that time , try
call it from
override func layoutSubviews() {
super.layoutSubviews()
self.containerViewdropShadow()
}
or
cell.layoutIfNeeded()
cell.containerView.dropShadow()

Left Align UICollectionViewCells only works when cells are not correct height, and stops working when scrolled

I am trying to left align the cells in a UICollectionView, and I have used a GitHub repo to try and obtain this, link here:
https://github.com/mischa-hildebrand/AlignedCollectionViewFlowLayout
I set up the CollectionView and its cells like so:
lazy var filterCollectionView: UICollectionView = {
let layout = AlignedCollectionViewFlowLayout(horizontalAlignment: .left, verticalAlignment: .center)
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.register(filterCell.self, forCellWithReuseIdentifier: "filterCellId")
cv.backgroundColor = .white
cv.isScrollEnabled = true
cv.dataSource = self
cv.delegate = self
cv.showsHorizontalScrollIndicator = false
return cv
}()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return genrArray.count
}
let genArray = ["Test123", "LongTextTest", "Short", "S", "#LongLongLongLong"]
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "filterCellId", for: indexPath) as! filterCell
cell.genText.text = genArray[indexPath.row]
cell.genText.sizeToFit()
cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: cell.genText.frame.width + 25, height: 24)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let label = UILabel(frame: .zero)
label.text = genArray[indexPath.row]
label.frame = CGRect(x: 0, y: 0, width: label.intrinsicContentSize.width, height: 0)
return CGSize(width: label.frame.width + 25, height: 18)
}
This did nothing to the cells, so I added these lines of code into ViewDidLoad():
let layout = AlignedCollectionViewFlowLayout(horizontalAlignment: .left, verticalAlignment: .center)
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
self.filterCollectionView.collectionViewLayout = layout
The line that actually made a change in the CollectionView was layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize. But this still did not achieve the desired effect. This squished my cells and made them have a height of much smaller than 18 (the size declared in sizeForItem. When loaded up, the collectionView looks like this:
But when I scroll through the collectionView, it reverts back to normal as if the left alignment layout is not even there. It then looks like this:
The cells are the correct shape in that image, but the alignment/layout/spacing is off.
What am I doing wrong?

UICollectionView equal cell spacing

I am displaying several images in a collection view. They are all the same orientation (landscape) except for one, which is portrait (see image below):
I am trying to make the portrait image more centered, so that everything is evenly spaced. Any ideas on how to do this?
PhotoGalleryViewController.swift:
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PhotoGalleryCell
let image = UIImage(named: images[indexPath.row])
cell.imageView.image = image
cell.captionLabel.text = captions[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var size = CGSize()
if (indexPath.row == 3)
{
size = CGSize(width: 450, height: 600)
}
else {
size = CGSize(width: 940, height: 600)
}
return size
}
My suggestion would be not altering the cell size based on the fact that a portrait image will be in item 3.
Instead update the layout of your cell so that whatever image it gets assigned to it's image view, it will centre the image in itself and centralise the label underneath the image.
In ViewDidLoad method put this code :
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionview.collectionViewLayout = layout
Also add this into cellForRow Methods:
cell.imageview.contentmode = .aspectfit
Hope it helps to resolve your issue.