Swift reusable NavigationLink

You can use modifier in extension, like below

extension Text {
    func styledLinkLabel<V: View & ShapeStyle>(with gradient: V) -> some View {
        self.fontWeight(.bold)
            .font(.title)
            .padding()
            .frame(minWidth: 0, maxWidth: 250, alignment: .center)
            .background(gradient)
            .foregroundColor(.white)
            .padding(2)
            .overlay(
                RoundedRectangle(cornerRadius:20)
                    .stroke(gradient, lineWidth:15)
            )
            .overlay(
                Image(systemName: "person.crop.circle.badge.plus")
                    .font(.largeTitle)
                    .padding(.leading)
                    .foregroundColor(.white)
                , alignment: .leading)
            .padding(5)
    }
}

and apply it

NavigationLink(destination: FairRentView(viewModel: FairRentViewModel(Amounts(), housemates: 2)))
{
    Text("2")
        .styledLinkLabel(with: gradient)
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top