gpt4 book ai didi

ios - 如何让 SwiftUI View 扩大可用空间?

转载 作者:行者123 更新时间:2023-12-01 21:51:23 28 4
gpt4 key购买 nike

我正在尝试使 SwiftUI View 扩展其可用空间。我有一个使用 MapKit 在屏幕上显示 map 的 MapView。我希望这张 map 可以扩展 VStack 中的可用空间。您可以看到它位于红色 View 上方和搜索栏下方。如果我使红色 View 的高度为 100,则 MapView 会缩小。如果我没有在红色 View 上设置高度,那么 MapView 会更大,但是红色 View 看起来不像我想要的那样。

我希望红色 View 的高度为 100,并且 MapView 填充搜索栏下方和红色 View 上方的所有可用高度。

内容 View :

struct ContentView: View {

@ObservedObject
var viewModel: HomeViewModel

var body: some View {
NavigationView {
ZStack {
ColorTheme.brandBlue.value.edgesIgnoringSafeArea(.all)
VStack {
CardView {
EditText(hint: "Search", text: self.$viewModel.searchText, textContentType: UITextContentType.organizationName)
}
MapView(annotations: self.$viewModel.restaurantAnnotations)
.cornerRadius(8)
CardView(height: 100) {
HStack {
Color.red

}
}
}
}
.navigationBarTitle("", displayMode: .inline)
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
}

卡片 View
struct CardView<Content>: View where Content : View {
var height: CGFloat = .infinity
var content: () -> Content
var body: some View {
content()
.padding(EdgeInsets.init(top: 0, leading: 8, bottom: 8, trailing: 8))
.background(Color.white.cornerRadius(8))
.shadow(radius: 2, x: 0, y: 1)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: height)
}
}

编辑文本
struct EditText: View {

var hint: String
@Binding
var text: String
var label: String = ""
var textContentType: UITextContentType? = .none
var keyboardType: UIKeyboardType = .default
var textSize: CGFloat = 16

var body: some View {
return VStack(alignment: .leading) {
Text(label).font(.system(size: 12)).bold()
.foregroundColor(ColorTheme.text.value)
HStack {
TextField(hint, text: $text)
.lineLimit(1)
.font(.system(size: textSize))
.textContentType(textContentType)
.keyboardType(keyboardType)
.foregroundColor(ColorTheme.text.value)
}
Divider().background(ColorTheme.brandBlue.value)
}
}
}

map View
struct MapView: UIViewRepresentable {

@Binding
var annotations: [MKAnnotation]

func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()
mapView.delegate = context.coordinator
return mapView
}


func updateUIView(_ view: MKMapView, context: Context) {
view.delegate = context.coordinator
view.addAnnotations(annotations)
if annotations.count == 1 {
let coords = annotations.first!.coordinate
let region = MKCoordinateRegion(center: coords, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
view.setRegion(region, animated: true)
}

}

func makeCoordinator() -> MapViewCoordinator {
MapViewCoordinator(self)
}
}

map View 协调器
class MapViewCoordinator: NSObject, MKMapViewDelegate {

var mapViewController: MapView

init(_ control: MapView) {
self.mapViewController = control
}


func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
//Custom View for Annotation
let identifier = "Placemark"
if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) {
annotationView.annotation = annotation
return annotationView
} else {
let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView.isEnabled = true
annotationView.canShowCallout = true
let button = UIButton(type: .infoDark)
annotationView.rightCalloutAccessoryView = button
return annotationView
}

return nil
}
}

如您所见, MapView 没有填满可用空间
Map View not filling available space

现在所有可用空间都已填满,但红色 View 高度不是 100
All space filled but red view height is not 100

最佳答案

这是一个解决方案(使用 Xcode 11.4 测试):

struct CardView<Content>: View where Content : View {
var height: CGFloat? = nil // << here !!

注意:定义的高度,即使是 .infinity ,通过请求 map View 的高度使您的上卡等效,因此他们划分了可用空间;未指定高度时,组件紧贴内容

关于ios - 如何让 SwiftUI View 扩大可用空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61604188/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com