Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grid layout with CollectionView in Swift

I would like to achieve this result: enter image description here

Searching around I found out that probably the way to do it is using UICollectionView, so no problem with that since there are many tutorials and questions on Stack Overflow. I have 3 questions:

  1. I cannot find anything about the "separators" (the line that divides all the boxes). I like that it doesn't touch the screen borders horizontally. Is it done programmatically?

  2. To divide the space equally in all devices (3 boxes/buttons horizontally) I found this answer answer. Is this the right approach?

  3. For the Blur effect I found this answer: How to implement UIVisualEffectView in UITableView with adaptive segues

For a TableView it would be:

if (!UIAccessibilityIsReduceTransparencyEnabled()) { tableView.backgroundColor = UIColor.clearColor() let blurEffect = UIBlurEffect(style: .Light) let blurEffectView = UIVisualEffectView(effect: blurEffect) tableView.backgroundView = blurEffectView } 

Can I do something like this?

     @IBOutlet var collectionView: UICollectionView!     if (!UIAccessibilityIsReduceTransparencyEnabled()) {     collectionView.backgroundColor = UIColor.clearColor()     let blurEffect = UIBlurEffect(style: .Light)     let blurEffectView = UIVisualEffectView(effect: blurEffect)     collectionView.backgroundView = blurEffectView     } 
like image 364
Mat Avatar asked Dec 09 '15 04:12

Mat


People also ask

What is CollectionView in Swift?

CollectionView is an object that presents the ordered collection of data items in the customizable layouts. It shows the data in the form of a grid layout. A collectionview is an instance of the UICollectionView class, which inherits the UIScrollView, which will be covered later in this tutorial.

What is CollectionView Flowlayout?

A flow layout is a type of collection view layout. Items in the collection view flow from one row or column (depending on the scrolling direction) to the next, with each row containing as many cells as will fit. Cells can be the same sizes or different sizes.

What is difference between Tableview and CollectionView?

1. The collection view is much more robust in terms of layout features and other things like animations. Tableiw is a simple list, which displays single-dimensional rows of data. It's smooth because of cell reuse and other magic.


1 Answers

Create the UICollectionViewController like this in a file that sub-classes from UICollectionViewController:

convenience override init() {     var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()     layout.itemSize = CGSizeMake(<width>, <height>)     // Setting the space between cells     layout.minimumInteritemSpacing = <Space between columns>     layout.minimumLineSpacing = <Space between rows>     return (self.init(collectionViewLayout: layout)) } 

In the viewDidLoad you an set the background color like this:

self.collectionView.backgroundColor = UIColor.orangeColor() 

My guess is you can set a background image like this:

self.collectionView?.backgroundColor = UIColor(patternImage: UIImage(named: "image.png")!) 

The blur effect that you found looks good. I am having trouble figuring out how it would work though. Probably set it using the backgroundView property.

I'll update if I find the answer.

Update:

Here is an idea of something that might work for blurring the cells.

Create a cocoa-touch class that sub-classes from UICollectionViewCell, then add this code to it:

convenience override init(frame: CGRect) {     self.init(frame: frame)     var blurEffect: UIVisualEffect     blurEffect = UIBlurEffect(style: .Light)     var visualEffectView: UIVisualEffectView     visualEffectView = UIVisualEffectView(effect: blurEffect)     visualEffectView.frame = self.maskView!.bounds     self.addSubview(visualEffectView) }  override func layoutSubviews() {     super.layoutSubviews()     self.maskView!.frame = self.contentView.bounds } 

Then in the CollectionViewController file, in the viewDidLoad, change this line of code:

self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 

Change UICollectionViewCell.self to <Name of CollectionViewCell file>.self

like image 142
Caleb Kleveter Avatar answered Oct 23 '22 13:10

Caleb Kleveter