SwiftUI View displayed with blue background

两盒软妹~` 提交于 2020-06-17 00:38:26

问题


I'm trying to reproduce the Apple tutorial(Composing Complex Interfaces) and I have a very weird problem. My CategoryItem view is being displayed as a blue frame.

If I remove the NavigationLink which wraps it, everything works fine but with that one it doesn't.

struct CategoryRow: View {
    var categoryName: String
    var items: [Landmark]

    var body: some View {

        VStack(alignment: .leading) {
            Text(self.categoryName)
                .font(.headline)
                .padding(.leading, 15)
                .padding(.top, 5)

            ScrollView(.horizontal, showsIndicators: false) {
                HStack(alignment: .top, spacing: 0) {
                    ForEach(self.items) { landmark in
                        NavigationLink(
                            destination: LandmarkDetail(
                                landmark: landmark
                            )
                        ) {
                            CategoryItem(landmark: landmark)
                        }
                    }
                }
            }.frame(height: 185)
        }
    }
}


回答1:


NavigationLink has a blue accent color by default, just call .accentColor(Color.clear) on it

Or you could try this:

NavigationView {
    NavigationLink(destination: Text("Detail view here")) {
        Image("YourImage")
    }
    .buttonStyle(PlainButtonStyle())
}

https://www.hackingwithswift.com/quick-start/swiftui/how-to-disable-the-overlay-color-for-images-inside-button-and-navigationlink




回答2:


renderingMode(.original) is what did it for me; .accentColor(Color.clear) made the image invisible (my best explanation here is because it didn't have a transparency).

NavigationView {
    NavigationLink(destination: Text("Detail view here")) {
        Image("YourImage")
            .renderingMode(.original)
    }
}

As the answer above mentioned, How to disable the overlay color for images inside Button and NavigationLink is a good write up as well.



来源:https://stackoverflow.com/questions/58713872/swiftui-view-displayed-with-blue-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!