Swiftui How to Programatically Adjust Spacing Between Images and Text

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

demo

HStack{
Image(systemName: item.leftImage)
.frame(width: 28)
Text(item.text)
}

Remove spacing between HStacks embedded in Stack

Since the Circles 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 HStacks.

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.

Sample Image

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)

demo

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

demo2

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))
}
}

Sample Image



Related Topics



Leave a reply



Submit