SwiftUI - Remove space between cells
I've had good luck with listRowInsets
struct ContentView: View {
var body: some View {
List {
Color.red
.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
Color.blue
.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
Color.yellow
.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
}
}
}
SwiftUI, remove space between views in a VStack?
Since you know that your HStack with the blue rectangles is going to be a height of 150
, you should constrain it to that using .frame(height: 150)
:
GeometryReader { geometry in
...
}
.padding()
.frame(height: 150) //Here
Otherwise, the GeometryReader
will occupy all available vertical space.
Re: your web dev comparison, check out the Xcode view hierarchy inspector. It's not exactly the same, but it's in the same vein: https://developer.apple.com/library/archive/documentation/ToolsLanguages/Conceptual/Xcode_Overview/ExaminingtheViewHierarchy.html
SwiftUI reduce spacing of rows in a list to null
Well, actually no surprise - .separatorStyle = .none
works correctly. I suppose you confused text background with cell background - they are changed by different modifiers. Please find below tested & worked code (Xcode 11.2 / iOS 13.2)
struct ContentView: View {
@State var data : [String] = ["first","second","3rd","4th","5th","6th"]
var body: some View {
VStack {
List {
ForEach(data, id: \.self)
{ item in
Text("\(item)")
.background(Color.yellow) // text background
.listRowBackground(Color.blue) // cell background
}
}
.onAppear { UITableView.appearance().separatorStyle = .none }
.onDisappear { UITableView.appearance().separatorStyle = .singleLine }
}
}
}
Update:
it's not possible to avoid the blue space between the yellow Texts?
Technically yes, it is possible, however for demo it is used hardcoded values and it is not difficult to fit some, while to calculate this dynamically might be challenging... anyway, here it is
it needs combination of stack for compression, content padding for resistance, and environment for limit:
List {
ForEach(data, id: \.self)
{ item in
HStack { // << A
Text("\(item)")
.padding(.vertical, 2) // << B
}
.listRowBackground(Color.blue)
.background(Color.yellow)
.frame(height: 12) // << C
}
}
.environment(\.defaultMinListRowHeight, 12) // << D
How to reduce space between LazyHGrid rows?
I was thinking about this incorrectly. You can create a LazyVGrid
with 7 columns and then just have a ForEach
loop that creates 30 different views which will wrap around to become rows.
struct Calendar: View {
// create 7 columns
var cols: [GridItem] = [
GridItem(spacing: 50),
GridItem(spacing: 50),
GridItem(spacing: 50),
GridItem(spacing: 50),
GridItem(spacing: 50),
GridItem(spacing: 50),
GridItem(spacing: 50)
]
var body: some View {
LazyVGrid(columns: cols, spacing: 5) {
ForEach((1...30), id: \.self) { num in
Text("\(num)")
}
}
.frame(width: .infinity, height: 450)
.padding()
}
}
How to remove blank space between two text views in SwiftUI
Put it conditionally, like
if let description = men.welcomeDescription {
Text(description)
.font(.caption)
.foregroundColor(.gray)
}
SwiftUI Remove List padding fand Row spacing?
For testing and showing that there is no problem with massive data, I pre loaded 10_000 row. is that enough?
your issues:
1.you should make your Item type Identifiable, then you can put out uuid from your list or ForEach.
2.you should not gave frame size to your row, they can take max space automatically.
3.you can put your button over your View, and save some more coding, like I did.
4.you should not use CGSize for offset, because you are just working on one dimension, and CGFloat is enough.
5.you should and must use LazyVStack if your data is massive as you said 400 is to many, then use LazyVStack for sure.
struct Item: Identifiable {
let id: UUID = UUID()
let value: String
}
struct ContentView: View {
@State private var items: [Item] = [Item]()
var body: some View {
ZStack {
Color.black.ignoresSafeArea()
ScrollView {
LazyVStack(spacing: 0) {
ForEach(items) {item in
RowView(stringOfText: item.value)
.frame(height: 120)
}
}
}
.background(Color.black)
.onAppear() { for _ in 0...10_000 { addNewData() } }
}
.overlay(addButton, alignment: .bottomTrailing)
.animation(.easeInOut)
}
var addButton: some View {
Button(action: { addNewData() }, label: {
Image(systemName: "plus.circle").foregroundColor(Color.white).font(Font.largeTitle.weight(Font.Weight.bold)).padding()
})
}
func addNewData() { items.append(Item(value: "item " + items.count.description)) }
}
struct RowView: View {
let stringOfText: String
@State private var offset: CGFloat = CGFloat()
var body: some View {
ZStack {
Color.blue
Text(stringOfText)
.foregroundColor(Color.white)
.padding()
}
.offset(x: offset)
.gesture(dragGesture)
}
var dragGesture: some Gesture {
DragGesture(minimumDistance: 25, coordinateSpace: .local)
.onChanged {
if (offset > 0 ){ return }
offset = $0.translation.width
}.onEnded {
if $0.translation.width < -100 {
offset = -100
} else {
offset = 0
}
}
}
}
SwiftUI - i'd like to remove space between back button and .navigationbartitle
Just remove redundant NavigationView
- it is needed only one in same view hierarchy, and obviously there is already some in parent view
struct RecipeIngredientsView: View {
let myArray = ["Dennis", "Tessa", "Peter", "Anna", "Tessa", "Klaus", "Xyan", "Zuhau", "Clown", "Brot", "Bauer"]
@State private var searchText = ""
@State private var showCancelButton: Bool = false
var body: some View {
NavigationView // << remove this one !!
{
iOS 15: Remove empty space before cells in UITableView
Check if you are only seeing this issue on iOS 15. If so, this may be caused by the newly introduced UITableView.sectionHeaderTopPadding
property. You will need to set this value to 0
in order to remove the spacing before section headings:
let tableView = UITableView()
tableView.sectionHeaderTopPadding = 0
// Etc.
This property is only available in iOS 15 so you will need an API check if building for earlier versions.
If you're not on iOS 15, this question has most of the answers to this issue.
How to remove the left and right Padding of a List in SwiftUI?
It looks like .listRowInsets
doesn't work for rows in a List
that is initialised with content
.
So this doesn't work:
List(items) { item in
ItemRow(item: item)
.listRowInsets(EdgeInsets())
}
But this does:
List {
ForEach(items) { item in
ItemRow(item: item)
.listRowInsets(EdgeInsets())
}
}
Related Topics
Why Does a Public Class/Struct in Swift Require an Explicit Public Initializer
Format String with Trailing Zeros Removed for X Decimal Places in Swift
If a Function Returns an Unsafemutablepointer Is It Our Responsibility to Destroy and Dealloc
How to Define an Enum as a Subset of Another Enum's Cases
Adding Nscoding as an Extension
Swift: How to Animate the Rowheight of a Uitableview
Converting String to Data in Swift 3.0
Variable P Passed by Reference Before Being Initialized
Swift3:How to Handle Precedencegroup Now Operator Should Be Declare with a Body
Swift + Nsviewcontroller Background Color (MAC App)
Swiftui - How to Set the Title of a Navigationview to Large Title (Or Small)
Checking If a Swift Class Conforms to a Protocol and Implements an Optional Function
How to Get Class Name of Parent Viewcontroller in Swift
Using Apple's New Audioengine to Change Pitch of Audioplayer Sound