我的目标是在地图上对注释进行聚类,并显示聚类中的项目数量,我没有 UIKit 经验并尝试避免它。是否可以仅使用 swiftUI 来做到这一点?如果不是如何减少UIKit的干预? 它应该是这样的
import SwiftUI
import MapKit
struct ContentView: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 43.64422936785126, longitude: 142.39329541313924),
span: MKCoordinateSpan(latitudeDelta: 1.5, longitudeDelta: 2)
)
var body: some View {
Map(coordinateRegion: $region, annotationItems: data) { annotation in
MapAnnotation(coordinate: annotation.coordinate) {
Image(systemName: "person.circle.fill")
.resizable()
.frame(width: 20, height: 20)
.foregroundColor(Color.purple)
}
}
.edgesIgnoringSafeArea(.all)
}
}
struct SampleData: Identifiable {
var id = UUID()
var latitude: Double
var longitude: Double
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(
latitude: latitude,
longitude: …
Run Code Online (Sandbox Code Playgroud) 你好,SO社区
我正在尝试从我的 SwiftUI 应用程序中的苹果手机联系人选项卡重新创建导航栏。
我尝试过.toolbar
并进行了修改,但确实无法重新创建它。我想将 TextField 替换为 SegmentPicker 并保存.navigationBarTitleDisplayMode(.inline)
. 但不知道是否可以仅使用 SwiftUI 来获取它,或者需要深入研究 UIKit。我不是 UIKit 的专家,我会很高兴获得任何帮助。我想在应用程序的精确屏幕中使用此导航栏,并且如果可能的话,不要更改我对应用程序其他部分的导航栏的偏好。我的代码:
import SwiftUI
struct NavBar: View {
@State var pickerOptions: Int = 0
var body: some View {
NavigationView {
ScrollView(.vertical) {
VStack {
Text("Hello, World!")
}
}
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
SegmentPicker
}
ToolbarItem(placement: .navigationBarTrailing) {
Image(systemName: "plus")
}
ToolbarItem(placement: .navigationBarLeading) {
Text("Groups")
}
}
}
}
private var SegmentPicker: some View {
VStack {
Text("Contacts").font(.body.weight(.semibold))
Picker("Options", selection: $pickerOptions) …
Run Code Online (Sandbox Code Playgroud) 乍一看问题很普通,但花了几个小时尝试不同的技术后,我仍然未能实现预期的行为...我想控制View
依赖于的旋转侧@State
。如果视图处于默认位置(V 形向上),它应该从右侧向下旋转。如果 V 形向下,它应该从左侧向上旋转。
我发现的另一件事是:在我的工作项目中,我使用 Image 代替 SF Symbol,并进行与代码示例中相同的修改,默认情况下它总是从右侧旋转,但使用 SF Symbol 时它从左侧旋转。如果您解释原因,我将不胜感激
struct Arrow: View {
@State var showView = false
var body: some View {
VStack {
Image(systemName: "chevron.up.circle")
.resizable()
.frame(width: 50, height: 50)
.rotationEffect(.degrees(showView ? 180 : 360))
.animation(Animation.easeInOut(duration: 0.3), value: showView)
}
.onTapGesture { showView.toggle() }
}
}
Run Code Online (Sandbox Code Playgroud)