SwiftUI + MapBox 集成搞乱了导航栏
SwiftUI + MapBox integration messes up navbar
我正在 SwiftUi 中实现一些 MapBox 功能,当使用默认的 NavigationBar 时,一切都按预期工作:
var body: some View {
NavigationView {
MapView(annotations: $annotations)
.centerCoordinate(.init(latitude: 37.791293, longitude: -122.396324))
.navigationBarTitle("Hello")
}
}
显示:
但是当尝试为导航栏设置 .inline 样式时,视图表现得很奇怪:
var body: some View {
NavigationView {
MapView(annotations: $annotations)
.centerCoordinate(.init(latitude: 37.791293, longitude: -122.396324))
.navigationBarTitle("Hello", displayMode: .inline)
//.navigationBarColor(.parqGreen)
}
}
添加颜色时:
知道为什么会这样吗?它是 MapBox 框架中的东西吗?
更新:添加了 MapView:
struct MapView: UIViewRepresentable {
@Binding var annotations: [MGLPointAnnotation]
private let mapView: MGLMapView = MGLMapView(frame: .zero, styleURL: MGLStyle.streetsStyleURL)
func makeUIView(context: UIViewRepresentableContext<MapView>) -> MGLMapView {
mapView.logoView.isHidden = true
mapView.attributionButton.isHidden = true
mapView.zoomLevel = 13
if let styleURL = URL(string: "mapbox://styles/morreke/cjkz2y4bq0kb12smmigszo70w") {
mapView.styleURL = styleURL
}
return mapView
}
func updateUIView(_ uiView: MGLMapView, context: UIViewRepresentableContext<MapView>) {
updateAnnotations()
}
private func updateAnnotations() {
if let currentAnnotations = mapView.annotations {
mapView.removeAnnotations(currentAnnotations)
}
mapView.addAnnotations(annotations)
}
func centerCoordinate(_ centerCoordinate: CLLocationCoordinate2D) -> MapView {
mapView.centerCoordinate = centerCoordinate
return self
}
}
更新 2:即使是最简单的实现也有相同的结果
struct MapView: UIViewRepresentable {
private let mapView: MGLMapView = MGLMapView()
func makeUIView(context: UIViewRepresentableContext<MapView>) -> MGLMapView {
return mapView
}
func updateUIView(_ uiView: MGLMapView, context: UIViewRepresentableContext<MapView>) {
}
}
将 Xcode 从 11.3.1 更新到 11.4.1 成功了。
我正在 SwiftUi 中实现一些 MapBox 功能,当使用默认的 NavigationBar 时,一切都按预期工作:
var body: some View {
NavigationView {
MapView(annotations: $annotations)
.centerCoordinate(.init(latitude: 37.791293, longitude: -122.396324))
.navigationBarTitle("Hello")
}
}
显示:
但是当尝试为导航栏设置 .inline 样式时,视图表现得很奇怪:
var body: some View {
NavigationView {
MapView(annotations: $annotations)
.centerCoordinate(.init(latitude: 37.791293, longitude: -122.396324))
.navigationBarTitle("Hello", displayMode: .inline)
//.navigationBarColor(.parqGreen)
}
}
添加颜色时:
知道为什么会这样吗?它是 MapBox 框架中的东西吗?
更新:添加了 MapView:
struct MapView: UIViewRepresentable {
@Binding var annotations: [MGLPointAnnotation]
private let mapView: MGLMapView = MGLMapView(frame: .zero, styleURL: MGLStyle.streetsStyleURL)
func makeUIView(context: UIViewRepresentableContext<MapView>) -> MGLMapView {
mapView.logoView.isHidden = true
mapView.attributionButton.isHidden = true
mapView.zoomLevel = 13
if let styleURL = URL(string: "mapbox://styles/morreke/cjkz2y4bq0kb12smmigszo70w") {
mapView.styleURL = styleURL
}
return mapView
}
func updateUIView(_ uiView: MGLMapView, context: UIViewRepresentableContext<MapView>) {
updateAnnotations()
}
private func updateAnnotations() {
if let currentAnnotations = mapView.annotations {
mapView.removeAnnotations(currentAnnotations)
}
mapView.addAnnotations(annotations)
}
func centerCoordinate(_ centerCoordinate: CLLocationCoordinate2D) -> MapView {
mapView.centerCoordinate = centerCoordinate
return self
}
}
更新 2:即使是最简单的实现也有相同的结果
struct MapView: UIViewRepresentable {
private let mapView: MGLMapView = MGLMapView()
func makeUIView(context: UIViewRepresentableContext<MapView>) -> MGLMapView {
return mapView
}
func updateUIView(_ uiView: MGLMapView, context: UIViewRepresentableContext<MapView>) {
}
}
将 Xcode 从 11.3.1 更新到 11.4.1 成功了。