How to wrap UICollectionView section and items with a border (shadow)? - ios

I want to make a border/shadow around each pair of section and items of my uicollectionview.
Further I want to round the top corners of the section view and the bottom corners of the items in the last row.
Do you know how to do this? - Is it necessary to write a custom UICollectionViewLayout?

If you want to add borders & rounded corners
cellName.layer.cornerRadius = cellName.frame.width / 2
cellName.layer.borderWidth = 5
cellName.layer.borderColor = UIColor.black
and I guess
add Shadow on UIView using swift 3
will help you with the shadows also add shadows to the contentView of the cell

Related

Corner radius and shadow on a view suddenly not working

Multiple placed in my app, I have views with both shadows and corner radii. I tried adding a new view, and suddenly the code I was reusing doesn't work anymore. I can only set a corner radius or a shadow, depending on what I put for masksToBounds. Here's the code I use for both the faulty view and my other views:
itemCountLabel.layer.masksToBounds = false
itemCountLabel.layer.cornerRadius = itemCountLabelSize / 2.0
itemCountLabel.layer.shadowColor = UIColor.black.cgColor
itemCountLabel.layer.shadowOpacity = 0.25
itemCountLabel.layer.shadowRadius = 5
itemCountLabel.layer.shadowOffset = CGSize(width: 4, height: 4)
contentView.addSubview(itemCountLabel)
It's not possible to implement as you've tried. Shadow is always applied outside the bounds of the UIView and the cornerRadius will not be visible without masking the bounds of UIView. So, better add a UIView behind the UILabel and to reuse the function write an extension of UIView that returns a UIView contains the view you want to apply the shadow.
Here you need to use two different views one to round the corners and the other behind it to show the shadow, As both these properties don’t work together because of the Mask To Bounds and Clip To Bounds features. As corner radius needs to clip the edges which might can contain the shadow.
So to have both of the things use a shadow view behind the view which you want to have rounded corners.

How to create space between 2 table view cell?

I have a TableViewCell which have the border at the surrounding.So what some space at the bottom of each cell.
Here is what I want :
I see a lot of question is work for cell which doesnt have any border.What I want is the space is outside the border at the bottom of every cell.
Now my cell is look like this
So how can I achieve the output that I stated in first picture?
It's easily accomplished with static/dynamic tableView cells & don't give the border to the cell itself but to the subview named TopView
tableViewCell
->ContentView
->TopView ---- with border height(static/dynamic)
->DownView transparent ---- height(10)
with
tableView.separatorStyle = .none

Rounded prototype corners iOS9.2

How would I change my cells in the UITableViewCells to have rounded corners? Kind of like this:
I like the gaps that are present, so is there away to adjust that as well?
In the ContentView of the UITableViewCell you can set, this lines the get the rounded corner.
self.layer.cornerRadius = 5.0
self.clipsToBounds = true
self.layer.masksToBounds = true
To give the gap, you need to set the default content view to Clear Color (to be transparent), and add a View inside the cell, with a smaller size then the cell.
It will look like this in the storyboard.

How to add border to UICollectionView content?

I would like to add a border to UICollectionView content so it can move with scroll and fit collection conent. Anyone has any idea how to achieve it? So far I am able to add a border to the whole collection but it is not what I'm looking for (pic).
Here, you have put a border to your collectionView.
If you wanna add a border to your cells, try this:
cell.layer.borderWidth = 1.0;
cell.layer.borderColor = UIColor.grayColor().CGColor;
Before this, make sure your cells are configured.

UICollectionView draw lines between the cells

Is there a way to draw lines between the cells in a UICollectionView? I'm not looking to add a border to each cell, that will result in drawing the lines only if there are any cells in the collection. What I'm looking for is something like UITableView's lines: they are there even if the table view has 0 cells.
I solved this by adding views to the collectionView.
I used the cell frames coordinates to determine where I wanted to draw.
However, there is probably a better way of doing this?
You can create a proper background for UITableView if your cells are of the same height. Draw the pattern image and apply it as a backgroundColor property:
UIImage *patternImage = [UIImage imageNamed:#"pattern.png"];
tableView.backgroundColor = [UIColor colorWithPatternImage:patternImage];
UPD:
Comment to this answer in incorrect. Background will scroll with tableView (or scrollView) content.

Resources