Use Sheet in SwiftUI to create a Detail View for Locations | SwiftUI Map App #7
private var learnMoreButton: some View {
Button {
viewModel.sheetLocation = location
} label: {
Text("Learn more")
.font(.headline)
.frame(maxWidth: 125, maxHeight: 30)
}
.buttonStyle(.borderedProminent)
}
sheetLocation
변수에 값을 할당.sheet(item: $viewModel.sheetLocation, onDismiss: nil) { location in
LocationDetailView(location: location)
}
sheetLocation
값이 널이 아닐 때 자동으로 모달 뷰가 띄워지는 구조private var backButton: some View {
Button {
viewModel.sheetLocation = nil
} label: {
Image(systemName: "xmark")
.font(.headline)
.padding(16)
.foregroundColor(.primary)
.background(.thickMaterial)
.cornerRadius(10)
.shadow(radius: 4)
.padding()
}
}
sheetLocation
에 직접 널을 할당함으로써 모달 시트를 내리기import SwiftUI
import MapKit
struct LocationDetailView: View {
@EnvironmentObject private var viewModel: LocationsViewModel
let location: LocationModel
var body: some View {
GeometryReader { geometry in
ScrollView {
VStack {
getImageSection(geometry: geometry)
.shadow(color: Color.black.opacity(0.3), radius: 20, x: 0, y: 10)
VStack(alignment: .leading, spacing: 20) {
titleSection
Divider()
descriptionSection
Divider()
mapLayer
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding()
}
}
.ignoresSafeArea()
.background(.ultraThinMaterial)
.overlay(
backButton, alignment: .topLeading
)
}
}
}
GeometryReader
사용private var mapLayer: some View {
Map(coordinateRegion: .constant(MKCoordinateRegion(
center: location.coordinates,
span: .init(latitudeDelta: 0.01, longitudeDelta: 0.01))),
annotationItems: [location]) { location in
MapAnnotation(coordinate: location.coordinates) {
LocationMapAnnotationView()
.shadow(radius: 10)
}
}
.allowsHitTesting(false)
.aspectRatio(1, contentMode: .fit)
.cornerRadius(30)
}
allowHitTesting
을 통해 false
를 전달aspectRatio
와 contentMode
를 통해 1 대 1 비율의 정사각형 구조