Swiftui how to programatically adjust spacing between images and text?
I would recommend to use explicit width for image frame, this will fit any symbol automatically and make it centred
HStack{
Image(systemName: item.leftImage)
.frame(width: 28)
Text(item.text)
}
Remove spacing between HStacks embedded in Stack
Since the Circle
s have no height constraint, they are taking up all of their available vertical space, even though the visible shape doesn't take up that space. Horizontally, they're limited by the width of the device/screen.
You can add .aspectRatio(contentMode: .fit)
to make them constrained vertically as well to only the space they need to take up:
Circle()
.aspectRatio(contentMode: .fit)
.foregroundColor(light.color)
Once that is done, if you also want to push them towards the top of the screen, you can add a Spacer
below the HStack
s.
Padding around objects
You need to set the spacing in your HStack
to 0.
It should look like this:
HStack(spacing: 0) {
Image(systemName: "faceid")
Image(systemName: "faceid")
}
Incorrect Spacing in SwiftUI View
You have the following:
Spacer()
before the following section of code:
if rightIcon != nil {
rightIcon!
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: iconWidth, height: iconHeight)
.foregroundColor(.secondary)
}
Removing the Spacer()
will give you the same pixels between the left icon and the right icon.
Why SwiftUI Image add an extra padding to enclosing VStack?
There are different default paddings between different UI elements, if you want explicit, use spacing
as below
VStack(alignment: .leading, spacing: 0) {
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
SwiftUI HStack text unaligned
A comment by Asperi contained this answer, which solved my problem!
https://stackoverflow.com/a/61984317/14410812
Putting .frame(width: 28)
on an image will set them to all be the same width, preventing this issue.
How Do I get uniform view size when using Image and SFSymbols in SwiftUI?
If you want to normalize the sizes, you could use a PreferenceKey
to measure the largest size and make sure that all of the other sizes expand to that:
struct ContentView: View {
let symbols = [ "camera", "comb", "diamond", "checkmark.square"]
@State private var itemSize = CGSize.zero
var body: some View {
HStack(spacing: 0) {
ForEach(Array(symbols), id: \.self) { item in
VStack {
Image(systemName: item).font(.largeTitle)
}
.padding()
.background(GeometryReader {
Color.clear.preference(key: ItemSize.self,
value: $0.frame(in: .local).size)
})
.frame(width: itemSize.width, height: itemSize.height)
.border(.red)
}.onPreferenceChange(ItemSize.self) {
itemSize = $0
}
}
.border(Color.black)
}
}
struct ItemSize: PreferenceKey {
static var defaultValue: CGSize { .zero }
static func reduce(value: inout Value, nextValue: () -> Value) {
let next = nextValue()
value = CGSize(width: max(value.width,next.width),
height: max(value.height,next.height))
}
}
Related Topics
iOS 8 Swift Audio Playback Execute Method on Completion
What Does the Underscore in a Function Declaration Do
Swift Generics and Protocols Not Working on Uikit [Possible Bug]
Tabview, Tabitem: Running Code on Selection or Adding an Ontapgesture
Swiftui: Unexpected Animation When Using a Non @State Var
Swift Tuple Has Unexpected Print Result
Swift: Download Image from Internet and Cache Them Doesn't Work Properly. Need Suggestions
How to Store the Progress of Progressview into Arraylist as One Element
Alamofire 4 Error Request 'Extra Argument in Call'
Swift Updating Screen in Between Steps of While Loop
How to Compare Two Strings to Check If They Have Same Characters Swift 4
Secure Text .Echosbullets Not Working for Password Field
C++ Dylib in Swift Project - Undefined Symbols for Function Exposed in Dylib
Swift Can Not Save .M3U8 File to Gallery
How to Take a Substring to the First Index of a Character
Interrupted Purchase Not Calling Delegate After Accepting T&C