Declaration
Initializers
2 SwiftUI initializers back <Link>. The props you pass pick one.
<Link
destination: string
>
children?: ReactNode
</Link>@MainActor @preconcurrency init(
destination: URL,
@ContentBuilder label: () -> Label
)destination*stringThe URL for the link.
childrenReactNodeContent. Strings become
Text; React Native views are laid out by Yoga and placed by SwiftUI.Swift
label: () -> Label
Also takes the common view props: modifiers, style and testID.
Overview
Create a link by providing a destination URL and a title. The title
tells the user the purpose of the link, and can be a string, a title
key that produces a localized string, or a view that acts as a label.
The example below creates a link to example.com and displays the
title string as a link-styled view:
Link("View Our Terms of Service",
destination: URL(string: "https://www.example.com/TOS.html")!)When a user taps or clicks a Link, the default behavior depends on the
contents of the URL. For example, SwiftUI opens a Universal Link in the
associated app if possible, or in the user's default web browser if not.
Alternatively, you can override the default behavior by setting the
EnvironmentValues.openURL environment value with a custom
OpenURLAction:
Link("Visit Our Site", destination: URL(string: "https://www.example.com")!)
.environment(\.openURL, OpenURLAction { url in
print("Open \(url)")
return .handled
})As with other views, you can style links using standard view modifiers
depending on the view type of the link's label. For example, a Text
label could be modified with a custom View.font(_:) or
View.foregroundStyle(_:) to customize the appearance of the link in
your app's UI.
From Apple’s SwiftUI documentation. Code samples there are Swift.
Relationships
Conforms to
- View
See also
More in Navigation.
- NavigationLinkA view that controls a navigation presentation.
- NavigationSplitViewA view that presents views in two or three columns, where selections in leading columns control presentations in subsequent columns.
- NavigationStackA view that displays a root view and enables you to present additional views over the root view.
- ShareLinkA view that controls a sharing presentation.
- TabThe content for a tab and the tab's associated tab item in a tab view.
- TabsTabbed navigation (SwiftUI TabView with Tab). Each Trigger declares a tab; the Content with the same value is its page.
- TabSectionA container that you can use to add hierarchy within a tab view.
- TabViewA view that switches between multiple child views using interactive user interface elements.