This content originally appeared on DEV Community and was authored by Anis Ali Khan
Build a Magazine-Style News Reader App in Swift
Author: Anis Ali Khan
Duration: β³ ~1 hour
Level: Intermediate
π Table of Contents
- Introduction π¬
- Project Setup ποΈ
- Understanding UICollectionView π
- Designing the News Layout ποΈ
- Creating the Data Model ποΈ
- Setting Up the Collection View ποΈ
- Implementing Compositional Layout ποΈ
- Adding Custom Cells π·οΈ
- Fetching & Displaying News Data π
- Adding Animations & Interactions π
- Testing & Debugging π οΈ
- Final Thoughts π
1οΈβ£ Introduction π¬
Welcome to this tutorial! Today, weβre building a Magazine-Style News Reader App using UICollectionViewCompositionalLayout in Swift. πβ¨
What Youβll Learn:
β
The basics of UICollectionView
β
How to use UICollectionViewCompositionalLayout
for complex grid designs
β
How to create dynamic, multi-section layouts
β
How to fetch and display real-world news data
β
How to add animations & interactions
Let's get started! π
2οΈβ£ Project Setup ποΈ
Step 1: Create a New Xcode Project
- Open Xcode β Click "Create a new Xcode project"
- Select App β Click Next
- Name it: MagazineReader
- Set Interface to Storyboard (or SwiftUI if preferred)
- Set Language to Swift
- Click Next β Choose a folder β Click Create
Step 2: Add Required Dependencies
Weβll use URLSession for network calls, but if you prefer, you can add Alamofire via Swift Package Manager (SPM).
3οΈβ£ Understanding UICollectionView π
UICollectionView
is a powerful class for building grid-based and custom layouts. It consists of:
- Cells β Individual content blocks
- Sections β Groupings of cells
- Layouts β Defines how cells are positioned
UICollectionViewCompositionalLayout vs Traditional Layouts
Feature | Flow Layout | Compositional Layout |
---|---|---|
Simplicity | β Easy | β Complex |
Flexibility | β Limited | β Highly Flexible |
Performance | β May lag | β Optimized |
Since we want a magazine-style grid, weβll use UICollectionViewCompositionalLayout
.
4οΈβ£ Designing the News Layout ποΈ
Our layout will have:
β
Featured News (Large Cells) β 1 large article at the top
β
Trending News (Grid Cells) β 2 articles side by side
β
Latest News (List Cells) β Scrolling list of articles
5οΈβ£ Creating the Data Model ποΈ
Create NewsItem.swift
import Foundation
struct NewsItem: Identifiable {
let id = UUID()
let title: String
let description: String
let imageURL: String
}
6οΈβ£ Setting Up the Collection View ποΈ
Step 1: Add UICollectionView to ViewController
- Open Main.storyboard
- Drag a
UICollectionView
into the ViewController - Set constraints to fill parent view
- Create an IBOutlet in
ViewController.swift
:
@IBOutlet weak var collectionView: UICollectionView!
Step 2: Register Cell Classes
collectionView.register(NewsCell.self, forCellWithReuseIdentifier: "NewsCell")
collectionView.register(FeaturedNewsCell.self, forCellWithReuseIdentifier: "FeaturedNewsCell")
7οΈβ£ Implementing Compositional Layout ποΈ
Step 1: Define Section Layouts
func createLayout() -> UICollectionViewCompositionalLayout {
return UICollectionViewCompositionalLayout { sectionIndex, _ in
switch sectionIndex {
case 0:
return self.createFeaturedLayout()
case 1:
return self.createTrendingLayout()
default:
return self.createListLayout()
}
}
}
8οΈβ£ Adding Custom Cells π·οΈ
Step 1: Create NewsCell.swift
import UIKit
class NewsCell: UICollectionViewCell {
static let identifier = "NewsCell"
let imageView = UIImageView()
let titleLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
contentView.addSubview(imageView)
contentView.addSubview(titleLabel)
// Set constraints
}
}
9οΈβ£ Fetching & Displaying News Data π
func fetchNews() {
let url = URL(string: "https://example.com/api/news")!
URLSession.shared.dataTask(with: url) { data, _, error in
if let data = data {
// Decode JSON and update collectionView
}
}.resume()
}
π Final Thoughts
You've successfully built a Magazine-Style News Reader App using UICollectionViewCompositionalLayout
! ππ₯
π Next Steps:
- Fetch real-world news using NewsAPI.org
- Add Dark Mode Support
- Implement Offline Caching
Happy coding! π¨π»
This content originally appeared on DEV Community and was authored by Anis Ali Khan

Anis Ali Khan | Sciencx (2025-04-02T18:06:40+00:00) Tutorial 28: Using Collection Views for Complex Layouts π°π±. Retrieved from https://www.scien.cx/2025/04/02/tutorial-28-using-collection-views-for-complex-layouts-%f0%9f%93%b0%f0%9f%93%b1/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.