Maksym Rohovskoi
Maksym Rohovskoi

Reputation: 103

How to change opacity of Navigation Link when clicked xcode / Swift UI

I have a Navigation Link set up with an image and by default when the user presses the Navigation Link, the image is reduced to around 30-40% opacity.

I'm wondering how to prevent this from happening and how to set the opacity to whatever I need it to be.

My code:

NavigationLink(destination:
    PostPageView()
    .navigationBarTitle(Text("Post"), displayMode: .inline)
    .foregroundColor(Color("blackAndWhite"))
  ){
    Image("partyyy").resizable().frame(height: 300)

  }

Image with example you can see below. Appreciate your help!

Image Example

Upvotes: 2

Views: 988

Answers (1)

Daniel
Daniel

Reputation: 588

You can create your own button style and add it to the NavigationLink.

.buttonStyle(MyButtonStyle())

Create a Struct for the custom style like this:

    struct MyButtonStyle: ButtonStyle {
    public func makeBody(configuration: MyButtonStyle.Configuration) -> some View {
        configuration.label
            .opacity(configuration.isPressed ? 1 : 1)

            // You can also add other animated properties
            .scaleEffect(configuration.isPressed ? 0.8 : 1)
    }
}

Upvotes: 5

Related Questions