SwiftUI NavigationView not see Image
This looks like SwiftUI bug of handling raster images. Please find below approach for workaround. Tested with Xcode 11.4 / iOS 13.4
This is an example of cell to be used for your goal. Of course in real case the image name and navigation link destination, etc., can be injected via constructor parameters for reuse purpose.
struct TestImageCell: View {
@State private var isActive = false
var body: some View {
Image("large_image") // raster !! image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 370, height: 200)
.cornerRadius(20.0)
.overlay(
Text("Press on me") // text is over
.foregroundColor(.white)
.font(.largeTitle)
)
.onTapGesture { self.isActive.toggle() } // activate link on image tap
.background(NavigationLink(destination: // link in background
Text("Detail view here"), isActive: $isActive) { EmptyView() })
}
}
SwiftUI : Using NavigationView leads to image and text gone
There is usually only one NavigationView
in an app. It is usually the outer most View
.
In this case put it above the VStack
in the ContentView
.
One of the most common exceptions are sheets.
Navigation View not working properly in SwiftUI on iPad
As I commented there should be only one NavigationView
, so here fixed ProductDetailView
with removed redundant NavigationView
.
Tested with Xcode 12
struct ProductDetailView: View {
var product: Product
var products: [Product] = productData
@State var showingPreview = false
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .center, spacing: 20) {
ProductHeaderView(product: product)
VStack(alignment: .leading, spacing: 15) {
Text(product.title)
.font(.largeTitle)
.fontWeight(.heavy)
Text(product.headline)
.font(.headline)
.multilineTextAlignment(.leading)
Text("Learn More About \(product.title)".uppercased())
.fontWeight(.bold)
.padding(0)
Text(product.description)
.multilineTextAlignment(.leading)
.padding(.bottom, 10)
}
.padding(.horizontal, 20)
.frame(maxWidth: 640, alignment: .center)
}
.navigationBarTitle(product.title, displayMode: .inline)
.navigationBarHidden(true)
}
.edgesIgnoringSafeArea(.top)
}
}
SwiftUI NavigationView Not Fully Extending In View
This is due to hack with appearance...
init() {
// UITabBar.appearance().isTranslucent = false // remove this line !!!
UITabBar.appearance().barTintColor = UIColor(Color.pacificBlue)
}
Alternate: replace entire appearance with configured opaque background
init() {
let newAppearance = UITabBarAppearance()
newAppearance.configureWithOpaqueBackground()
newAppearance.backgroundColor = UIColor(Color.pacificBlue)
UITabBar.appearance().standardAppearance = newAppearance
}
Display system back button on SwiftUI NavigationView with no previous NavigationLink
You may try the following:
private var backButton: some View {
Button(action: {}) {
Image(systemName: "chevron.left")
.scaleEffect(0.83)
.font(Font.title.weight(.medium))
}
}
Optionally you can apply .offset
as well, but this may not adapt properly for larger accessibility font sizes:
.offset(x: -7, y: 0)
How do I navigate to my SecondView by tapping image button?
When you want to navigate, you simply use NavigationLink
without a Button
:
NavigationLink(destination: SecondView(), label: {
Image("radio-on-button")
.renderingMode(.original)
.resizable()
.frame(width: 75, height: 75)
.foregroundColor(.red)
.padding(.horizontal)
})
Button
is used to execute some action.
Related Topics
Extract Reality Composer Scene for Arquicklook
Swiftui Vertically Misaligned Text
Swift Equivalent of Unity3D Coroutines
Do Protocols Have an Effect on The Retain Count
Realm Write Transaction Failing, Despite Being in Transaction
Applying Impulses in Spritekit
Typecase Regular Swift Function to Curry Function
Error: Variable with Getter/Setter Cannot Have an Initial Value
Add Catextlayer on Top of Nsimageview
Swift Set UIbutton Setbordercolor in Storyboard
Wkwebview Calayer to Image Exports Blank Image
Create Custom Action in a Class for Use in Interface Builder
Swift Initialization Rule Confusion
Can't Override UItableviewdatasource and UItableviewdelegate
Nskeyedunarchiver Decodeobjectforkey: Cannot Decode Object of Class for Key (Ns.Objects)