HStack with SF Symbols Image not aligned centered
This is what it's going on.
The Image
views are not resizing.
It looks like they're not aware of their intrinsic content size, or maybe it reports the wrong value.
To fix it:
struct ContentView : View {
var body: some View {
HStack(alignment: .center, spacing: 10.0) {
Image(systemName: "cloud.sun")
.resizable()
.aspectRatio(contentMode: .fit)
.background(Color.red)
Image(systemName: "cloud")
.resizable()
.aspectRatio(contentMode: .fit)
.background(Color.yellow)
Image(systemName: "cloud.bolt")
.resizable()
.aspectRatio(contentMode: .fit)
.background(Color.pink)
Text("Text").background(Color.green)
}
.frame(width: 250, height: 50)
.background(Color.gray)
.font(.title)
}
}
...make the Images
resizable, and also make sure the aspect ratio is set to .fit
, or they will stretch.
Set also frame on the HStack
or it will expand to fill the whole screen.
@MartinR suggested an even better solution - creating the images via UIImage
- see his comment below.
struct ContentView : View {
var body: some View {
HStack {
Image(uiImage: UIImage(systemName: "cloud.sun")!)
.background(Color.red)
Image(uiImage: UIImage(systemName: "cloud")!)
.background(Color.yellow)
Image(uiImage: UIImage(systemName: "cloud.bolt")!)
.background(Color.pink)
Text("Text").background(Color.green)
}
.background(Color.gray)
.font(.title)
}
}
Output:
How do I center these images in a straight line inside my HStack in SwiftUI
Wrapping everything in a geometryreader and assigning relative width to your textviews should do the trick:
var body: some View {
//decide which image to show
GeometryReader{ readerProxy in <- add this
HStack(alignment: .center){
Text(dailyData.time)
.fontWeight(.semibold)
.foregroundColor(Color(UIColor.white))
.font(.system(size: 16))
.frame(width: readerProxy.size.width / 3, alignment: .left) <- add this
Spacer()
Image(dailyData.icon)
.resizable()
.scaledToFit()
.frame(width: 25, height: 25, alignment: .center)
.multilineTextAlignment(.leading)
Spacer()
HStack(spacing: 10){
Text(dailyData.lowtemp + "°")
.fontWeight(.regular)
.foregroundColor(Color(UIColor.white))
.font(.system(size: 16))
Text(dailyData.hightemp + "°")
.fontWeight(.regular)
.foregroundColor(Color(UIColor.white))
.font(.system(size: 16))
}
.frame(width: readerProxy.size.width / 3, alignment: .right) <- add this
}
}
}
SwiftUI align SF Symbol as text with a text
I don’t know if you can force the image to automatically be centred in all cases, but you can add a .baselineOffset()
modifier to the image-in-text to shift the image upwards by a fixed amount. For example:
Text(Image(systemName: "circle.hexagongrid.circle"))
.font(.caption)
.foregroundColor(.pink)
.baselineOffset(3.0)
+
Text("A long text as example")
.font(.title2).bold()
.foregroundColor(.pink)
Once you have hit upon the right offset amount for the default text size, you might be able to accommodate accessibility variations by making the offset size relative to a base size, e.g.
Struct MyView: View {
@ScaledMetric(relativeTo: .title2) var baselineOffset: CGFloat = 3.0
// and in your body…
.baselineOffset(baselineOffset)
How do i align an image with text in an HStack?
You just need to remove padding from Text("500")
and set to HStack
Please try with below code and let me know it's works for you
VStack(alignment: .center, spacing: 0) {
Image("ProductLogo")
.resizable()
.aspectRatio(contentMode: .fit)
.clipped()
.padding()
.layoutPriority(1)
Text("Text Here")
.font(.title2)
.fontWeight(.bold)
.foregroundColor(.black)
.minimumScaleFactor(0.5)
.multilineTextAlignment(.center)
.padding(.leading, 5)
.padding(.trailing, 5)
.padding(.bottom, 5)
Text("Subtitle")
.font(.headline)
.foregroundColor(.gray)
.lineLimit(2)
.padding(1)
//HERE IS WHAT NEEDS TO BE ALIGNED
HStack {
Image("CoinImage")
.resizable()
.frame(width: 35, height: 35, alignment: .center)
Text("500")
.font(.system(size: 20))
}.padding(.top, 10)
//END OF HSTACK
}
How to make SF Symbols in SwiftUI have the same width
A workaround for the issue. However, the problem is that it does not work well with dynamic font sizing.
HStack {
Image(systemName: "square.and.arrow.up")
.font(Font.system(size: 18, weight: .medium, design: .monospaced))
.foregroundColor(Color("secondary"))
}
.frame(minWidth: 30)
Aligning texts and images in SwiftUI
Images can have baselines, and although many times they are equal to zero, there are some other cases (like "Cloudy"), when they're not. If you set the baseline to zero, then it will center with the text:
struct ContentView: View {
var body: some View {
HStack(alignment: .center) {
Text("Cloudy").background(Color.yellow)
Image(uiImage: UIImage(systemName: "cloud.heavyrain")!.withBaselineOffset(fromBottom: 0))
.background(Color.blue)
}
.font(.largeTitle)
.border(Color.red)
}
}
Related Topics
Blue Highlighting/Focus Ring on Catalyst App
Why Is Forwarding Variadic Parameters Invalid
How to Present Different Navigation Title When Large Title Collapse
Clgeocoder in Swift - Unable to Return String When Using Reversegeocodelocation
Hex String to Character in Pure Swift
Multi-Component Picker (Uipickerview) in Swiftui
How to Concatenate Optional Swift Strings
How to Get User Input in Apple's Swift Language in a Command Line Tool
Failed to Get Descriptors for Extensionbundleid
How to Animate Transition Between Views in Swiftui
Key-Value Coding (Kvc) with Array/Dictionary in Swift
How to Append a Character to a String in Swift
Understanding the Removerange(_:) Documentation
Nsbundle.Mainbundle().Urlforresource("Bach1", Withextension: "Jpg") Returning Null
Realm Mobile Platform, How to Connect While Offline
Display All Available Wifi Connections with Swift in Os X
How to Nskeyedunarchiver.Unarchiveobject
Why Does an @Objc Enum Have a Different Description Than a Pure Swift Enum