I have a SwiftUI app. The main view lists the latest articles from a blog. I want the first article to be presented with larger text and a larger image, with the rest of the articles listed below.
The code that I have works, but there's a problem as the featured article row overflows the width of the container. This means that the image and text are off the edge of the screen to the left, and the navigation chevron is off the screen to the right. The "regular" article rows display as expected.
Here is the code for the main view and then then FeatureArticleRow.
struct LatestListView: View {
@StateObject private var vm = LatestViewModel()
@State private var showDetail = false
@State private var selectedArticle: String?
code
var body: some View {
NavigationView {
Group {
if vm.isLoading {
ProgressView("Data is loading…")
} else if let error = vm.error {
Text(error)
} else {
List {
if let firstArticle = vm.articles.first {
NavigationLink(destination: ArticleDetailView(slug: firstArticle.slug)) {
FeatureArticleRow(article: firstArticle)
}
}
ForEach(vm.articles.dropFirst(), id: \.slug) { article in
NavigationLink {
ArticleDetailView(slug: article.slug)
} label: {
ArticleRow(article: article)
}
}
}
.listStyle(.plain)
}
}
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Text("Latest articles")
.font(.custom("Raleway-Black", size: 28))
.font(.system(size: 28, weight: .bold, design: .default)) // Fallback for dynamic type
.minimumScaleFactor(0.5) // Optional: Set a minimum scale factor
.foregroundColor(.primary)
}
}
}
.task { await vm.load() }
}
}
And ...
struct FeatureArticleRow: View {
let article: ArticleSummary
code
var body: some View {
VStack(alignment: .leading) {
AsyncImage(url: article.thumbImageURL) { phase in
if let image = phase.image {
image
.resizable()
.aspectRatio(3/2, contentMode: .fill) // Maintain 3:2 aspect ratio
.frame(maxWidth: .infinity) // Fill the entire container width
.clipped() // Clip any overflow
} else {
Color.gray.opacity(0.2) // Placeholder
.frame(maxWidth: .infinity) // Set placeholder to fill width
.aspectRatio(3/2, contentMode: .fill) // Maintain 3:2 aspect ratio for placeholder
.clipped() // Clip any overflow
}
}
Text(article.title ?? "Untitled")
.font(Font.custom("Raleway-Bold", size: 16))
.foregroundColor(.primary)
.lineLimit(2)
.multilineTextAlignment(.leading)
Text(article.description ?? "")
//.font(.subheadline)
.font(Font.custom("Raleway-Regular", size: 15))
.foregroundColor(.secondary)
.lineLimit(5)
.multilineTextAlignment(.leading)
}
.padding(.vertical, 5)
}
}
Please refactor the code to simplify as much as possible AND fix the layout issue that I described.
Assistant
Refactored Code
The main issue with your code is that you're using a List which is meant for displaying rows of data in a scrollable list. However, for your featured article, you want a different layout that doesn't conform to the traditional list row structure.
Instead, you can use a VStack with the featured article at the top and a List below it. Here's the refactored code:
swift
struct LatestListView: View {
@StateObject private var vm = LatestViewModel()
@State private var showDetail = false
@State private var selectedArticle: String?
var body: some View {
NavigationView {
Group {
if vm.isLoading {
ProgressView("Data is loading…")
} else if let error = vm.error {
Text(error)
} else {
ScrollView {
if let firstArticle = vm.articles.first {
NavigationLink(destination: ArticleDetailView(slug: firstArticle.slug)) {
FeatureArticleRow(article: firstArticle)
.padding(.horizontal) // Add horizontal padding
}
}
List {
ForEach(vm.articles.dropFirst(), id: \.slug) { article in
NavigationLink {
ArticleDetailView(slug: article.slug)
} label: {
ArticleRow(article: article)
}
}
}
.listStyle(.plain)
}
}
}
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Text("Latest articles")
.font(.custom("Raleway-Black", size: 28))
.font(.system(size: 28, weight: .bold, design: .default)) // Fallback for dynamic type
.minimumScaleFactor(0.5) // Optional: Set a minimum scale factor
.foregroundColor(.primary)
}
}
}
.task { await vm.load() }
}
}
struct FeatureArticleRow: View {
let article: ArticleSummary
var body: some View {
VStack(alignment: .leading) {
AsyncImage(url: article.thumbImageURL) { phase in
if let image = phase.image {
image
.resizable()
.aspectRatio(3/2, contentMode: .fit) // Changed to .fit to avoid overflow
} else {
Color.gray.opacity(0.2) // Placeholder
.aspectRatio(3/2, contentMode: .fit) // Maintain 3:2 aspect ratio for placeholder
}
}
VStack(alignment: .leading, spacing: 4) {
Text(article.title ?? "Untitled")
.font(.custom("Raleway-Bold", size: 24)) // Increased font size for featured article
.foregroundColor(.primary)
.lineLimit(2)
Text(article.description ?? "")
.font(.custom("Raleway-Regular", size: 18)) // Increased font size for featured article
.foregroundColor(.secondary)
.lineLimit(5)
}
.padding(.top, 8)
Image(systemName: "chevron.right")
.font(.title2)
.frame(maxWidth: .infinity, alignment: .trailing)
.foregroundColor(.secondary)
}
}
}
Changes Made:
Replaced the List with a ScrollView to allow for a custom layout at the top.
Moved the featured article outside of the List and added horizontal padding to prevent it from touching the edges of the screen.
Changed the contentMode of the AsyncImage from .fill to .fit to prevent the image from overflowing its bounds.
Increased the font sizes of the title and description in the FeatureArticleRow to make them more prominent.
Added a chevron icon at the bottom of the FeatureArticleRow to indicate that it's a navigable link, just like the rows in the List.
Removed the .clipped() modifier as it's not necessary with .fit content mode.
Simplified the layout of the texts in FeatureArticleRow by using a nested VStack with spacing.
This should fix the layout issue and provide a cleaner, more readable code structure. The featured article now has a larger image and text, and the rest of the articles are listed below in a traditional list format.