NavigationLink is grayed out and does not perform any action
Your ContentView should have a NavigationView for NavigationLink to work, and be enclosed in some 'element', here a VStack
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView()) {
Text("Show Details")
}
}
}
}
}
Hope this helps!
NavigationLink in SwiftUI not working anymore
I can see from the comments that you've found out it will only work in a NavigationView
and are now wondering why. It only matters that your view is embedded in a NavigationView
directly above it the View hierarchy. For example, this code would work:
struct FirstView: View {
var body: some View {
NavigationView {
NavigationLink(label: "Go to next view", destination: NextView())
}
}
}
struct NextView: View {
...
While this won't:
struct FirstView: View {
@State var modalPresented = false
var body: some View {
NavigationView {
Button("Show fullscreen cover"){
modalPresented = true
}
}
.fullScreenCover(isPresented: $modalPresented, content: SecondView())
}
}
struct SecondView: View {
var body: some View {
NavigationLink(label: "Go to next view", destination: NextView())
// Doesn't work because the view is in a fullScreenCover and therefore not a part of the NavigationView.
}
}
Why does NavigationLink buttons appear disabled in a custom UIViewControllerRepresentable wrapper
I spent some time with your code. I think I understand what the problem is, and found a workaround.
The issue is, I think, that for NavigationLink
to be enabled, it needs to be inside a NavigationView
. Although yours is, it seems the "connection" is lost with UIHostingController
. If you check the UIHostingController.navigationController
, you'll see that it is nil.
The only solution I can think of, is having a hidden NavigationLink
outside the SwiftyUIScrollView
that can be triggered manually (with its isActive
parameter). Then inside your SwiftyUIScrollView
, you should use a simple button that when tapped, changes your model to toggle the NavigationLink's isActive
binding. Below is an example that seems to work fine.
Note that NavigationLink
's isActive
has a small bug at the moment, but it will probably be fixed soon. To learn more about it: https://swiftui-lab.com/bug-navigationlink-isactive/
window.rootViewController = UIHostingController(rootView: ContentView().environmentObject(MyModel()))
import SwiftUI
class MyModel: ObservableObject {
@Published var navigateNow = false
}
struct TestData {
var id : Int
var text: String
}
struct ContentView: View {
@EnvironmentObject var model: MyModel
var contentArray: [TestData] = [TestData(id: 0, text: "Test 1"), TestData(id: 1, text: "Test 2"), TestData(id: 2, text: "TEst 3"), TestData(id: 4, text: "Test 4")]
var body: some View {
NavigationView {
GeometryReader { g in
ZStack{
NavigationLink(destination: Text("Destination View"), isActive: self.$model.navigateNow) { EmptyView() }
SwiftyUIScrollView(axis: .horizontal, numberOfPages: self.contentArray.count, pagingEnabled: true, pageControlEnabled: true, hideScrollIndicators: true) {
HStack(spacing: 0) {
ForEach(self.contentArray, id: \.id) { item in
TestView(data: item)
.frame(width: g.size.width, height: g.size.height)
}
}
}.frame(width: g.size.width)
}.frame(width: g.size.width, height: g.size.height)
.navigationBarTitle("Test")
}
}
}
}
struct TestView: View {
@EnvironmentObject var model: MyModel
var data: TestData
var body: some View {
GeometryReader { g in
VStack {
HStack {
Spacer()
}
Text(self.data.text)
Text(self.data.text)
VStack {
Button("Pseudo-Navigation Link Test") {
self.model.navigateNow = true
}
}
Button(action: {
print("Do something")
}) {
Text("Button")
}
}.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.yellow)
}
}
}
The other thing is your use of AnyView
. It comes with a heavy performance price. It is recommended you only use AnyView
with leaf views (not your case). So I did managed to refactor your code to eliminate the AnyView
. See below, hope it helps.
import SwiftUI
enum DirectionX {
case horizontal
case vertical
}
struct SwiftyUIScrollView<Content: View>: UIViewControllerRepresentable {
var content: () -> Content
var axis: DirectionX
var numberOfPages = 0
var pagingEnabled: Bool = false
var pageControlEnabled: Bool = false
var hideScrollIndicators: Bool = false
init(axis: DirectionX, numberOfPages: Int,
pagingEnabled: Bool,
pageControlEnabled: Bool,
hideScrollIndicators: Bool,
@ViewBuilder content: @escaping () -> Content) {
self.content = content
self.numberOfPages = numberOfPages
self.pagingEnabled = pagingEnabled
self.pageControlEnabled = pageControlEnabled
self.hideScrollIndicators = hideScrollIndicators
self.axis = axis
}
func makeUIViewController(context: Context) -> UIScrollViewController<Content> {
let vc = UIScrollViewController(rootView: self.content())
vc.axis = axis
vc.numberOfPages = numberOfPages
vc.pagingEnabled = pagingEnabled
vc.pageControlEnabled = pageControlEnabled
vc.hideScrollIndicators = hideScrollIndicators
return vc
}
func updateUIViewController(_ viewController: UIScrollViewController<Content>, context: Context) {
viewController.hostingController.rootView = self.content()
}
}
class UIScrollViewController<Content: View>: UIViewController, UIScrollViewDelegate {
var axis: DirectionX = .horizontal
var numberOfPages: Int = 0
var pagingEnabled: Bool = false
var pageControlEnabled: Bool = false
var hideScrollIndicators: Bool = false
lazy var scrollView: UIScrollView = {
let view = UIScrollView()
view.delegate = self
view.isPagingEnabled = pagingEnabled
view.showsVerticalScrollIndicator = !hideScrollIndicators
view.showsHorizontalScrollIndicator = !hideScrollIndicators
return view
}()
lazy var pageControl : UIPageControl = {
let pageControl = UIPageControl()
pageControl.numberOfPages = numberOfPages
pageControl.currentPage = 0
pageControl.tintColor = UIColor.white
pageControl.pageIndicatorTintColor = UIColor.gray
pageControl.currentPageIndicatorTintColor = UIColor.white
pageControl.translatesAutoresizingMaskIntoConstraints = false
pageControl.isHidden = !pageControlEnabled
return pageControl
}()
init(rootView: Content) {
self.hostingController = UIHostingController<Content>(rootView: rootView)
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var hostingController: UIHostingController<Content>! = nil
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
self.makefullScreen(of: self.scrollView, to: self.view)
self.hostingController.willMove(toParent: self)
self.scrollView.addSubview(self.hostingController.view)
self.makefullScreen(of: self.hostingController.view, to: self.scrollView)
self.hostingController.didMove(toParent: self)
view.addSubview(pageControl)
pageControl.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
pageControl.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
pageControl.heightAnchor.constraint(equalToConstant: 60).isActive = true
pageControl.widthAnchor.constraint(equalToConstant: 200).isActive = true
}
func makefullScreen(of viewA: UIView, to viewB: UIView) {
viewA.translatesAutoresizingMaskIntoConstraints = false
viewB.addConstraints([
viewA.leadingAnchor.constraint(equalTo: viewB.leadingAnchor),
viewA.trailingAnchor.constraint(equalTo: viewB.trailingAnchor),
viewA.topAnchor.constraint(equalTo: viewB.topAnchor),
viewA.bottomAnchor.constraint(equalTo: viewB.bottomAnchor),
])
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let currentIndexHorizontal = round(scrollView.contentOffset.x / self.view.frame.size.width)
let currentIndexVertical = round(scrollView.contentOffset.y / self.view.frame.size.height)
switch axis {
case .horizontal:
self.pageControl.currentPage = Int(currentIndexHorizontal)
break
case .vertical:
self.pageControl.currentPage = Int(currentIndexVertical)
break
default:
break
}
}
}
SwiftUI - NavigationLink is not working with a button
NavigationLink
is itself a button, actually, so you introduce some kind of conflict. Instead you can use link just with additional tap gesture handler, like
NavigationLink(destination: Text("\(self.sum)")) {
Text("Add Two Numbers")
.padding()
.foregroundColor(.blue)
.background(isTapped ? Color.orange : Color.gray)
.font(.title)
.border(Color.blue, width: 5)
.shadow(radius: 10)
}
.simultaneousGesture(TapGesture().onEnded{
print("I am here in the action")
self.isTapped.toggle()
UIApplication.shared.endEditing()
if let num1 = Double(self.number1), let num2 = Double(self.number2){
print("I am here")
self.sum = num1 + num2
print("\(self.sum)")
}
})
SwiftUI NavigationLink not behaving correctly when buttons are close together
As your buttons are in a Form
you need to change their style to PlainButtonStyle
:
Button(action: {
self.showPasswordStr.toggle()
}) {
Image(systemName: showPasswordStr ? "eye.slash" : "eye")
}
.buttonStyle(PlainButtonStyle()) // <- add style
Button(action: { self.showCheckView.toggle() }) {
Image(systemName: "checkmark.circle")
}
.buttonStyle(PlainButtonStyle()) // <- add style
Related Topics
Implementing a Hash Combiner in Swift
Pattern Matching in a Swift for Loop
Implement Protocol Through Extension
Animate an Skspritenode with Textures That Have a Size Different from the Original
How to Make a Custom Mkannotationview with Xib
Possible to Write Swift Println Logs into File Too
Referencing Self in Super.Init
Why I Can Not Use Equatable Function in My View When the View Can Use It in Swiftui
How to Combine Two Nsdictionary in Swift
Swift: Generic Overloads, Definition of "More Specialized"
Creating a Subclass of Skshapenode
How to Create a Portal Effect in Arkit Just Using the Scenekit Editor
Swift: How to Create a Predicate with an Int Value
Dyld: Library Not Loaded, App Requires Afnetworking 2.0.0 But Provides Version 1.0.0