When I use as system image everything works fine, but I have a png image of size 512x512 and I want to make it fit inside a navigationBarItem. The image just takes over almost all the screen with the following code:
var body: some View {
NavigationView {
Text("Main")
.navigationBarTitle("Title", displayMode: .inline)
.navigationBarItems(leading:
Button(action: {
print("button pressed")
})
{
Image("menu")
.renderingMode(.template)
.resizable()
.aspectRatio(contentMode: .fit)
.scaledToFit()
}
)
.navigationViewStyle(StackNavigationViewStyle())
}
}
GeometryReader
gives you the area designated by the parent. I found that the designated space is quite small but if you triple it, it fits well.
import SwiftUI
struct NavItemImage: View {
var body: some View {
NavigationView {
Text("Main")
.navigationBarTitle(Text("Title").font(.largeTitle), displayMode: .inline)
.navigationBarItems(leading:
GeometryReader{geo in
Button(action: {
print("button pressed")
})
{
Image("menu")
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fit)
.scaledToFit()
.frame(width: geo.size.width * 3, height: geo.size.height * 3, alignment: .leading)
}
}
)
.navigationViewStyle(StackNavigationViewStyle())
}
}
}