【问题标题】:SwiftUI - i'd like to remove space between "back button" and .navigationbartitleSwiftUI - 我想删除“后退按钮”和 .navigationbartitle 之间的空格
【发布时间】:2020-12-05 18:31:01
【问题描述】:

我想删除我的后退按钮(“Rezept hinzufügen”)和我的导航栏标题(“Suche”)之间的空间......我不知道为什么会有这个空间,所以我需要你的群体智能。 :)

我尝试了什么?

  1. 使用 ZStack,可能之前的 NavigationView 存在此错误
  2. 添加 Spacer()
  3. 重新创建整个视图

现在我卡住了……

我认为最简单的方法是通过视频向您展示我的问题...

Here you can see my problem

这是我的代码...

import SwiftUI
extension UIApplication
{
    func endEditing(_force : Bool)
    {
        self.windows
            .filter{$0.isKeyWindow}
            .first?
            .endEditing(_force)
    }
}

struct ResignKeyboardOnDragGesture: ViewModifier
{
    var gesture = DragGesture().onChanged{_ in UIApplication.shared.endEditing(_force: true)}
    
    func body(content: Content) -> some View
    {
        content.gesture(gesture)
    }
}

extension View
{
    func resignKeyboardOnDragGesture() -> some View
    {
        return modifier(ResignKeyboardOnDragGesture())
    }
}
/*
    Zutaten pflegen Button, zum hinzufügen von Zutaten zu einem Rezept.
 **/
struct RecipeIngredientsView: View {
    let myArray = ["Dennis", "Tessa", "Peter", "Anna", "Tessa", "Klaus", "Xyan", "Zuhau", "Clown", "Brot", "Bauer"]
    @State private var searchText = ""
    @State private var showCancelButton: Bool = false
    
    var body: some View {
        NavigationView
        {
            VStack
            {
                HStack
                {
                    HStack
                    {
                        Image(systemName: "magnifyingglass")
                        TextField("Suche", text: $searchText, onEditingChanged: { isEditing in self.showCancelButton = true}, onCommit: {
                            print("onCommit")
                        }).foregroundColor(.primary)
                        
                        Button(action: {
                            self.searchText = searchText
                            }){
                            Image(systemName: "xmark.circle.fill").opacity(searchText == "" ? 0 : 1)
                            }
                    }.padding(EdgeInsets(top: 8, leading: 6, bottom: 8, trailing: 6))
                    .foregroundColor(.secondary)
                    .background(Color(.secondarySystemBackground))
                    .cornerRadius(10.0)
                    
                    if showCancelButton {
                        Button("Abbrechen")
                            {
                            UIApplication.shared.endEditing(_force: true)
                            self.searchText = ""
                            self.showCancelButton = false
                            }
                        .foregroundColor(Color(.systemBlue))
                    }
                }
                .padding(.horizontal)
                .navigationBarHidden(showCancelButton)
                
                //Gefilterte Liste der Namen aus meinem Array
                List {
                    ForEach(myArray.filter{$0.hasPrefix(searchText) || searchText == ""}, id:\.self)
                        {
                        searchText in Text(searchText)
                    }
                }
                .navigationBarTitle(Text("Suche"))
                .resignKeyboardOnDragGesture()
                
            }
        }
    }
}

感谢您的帮助! :-)

【问题讨论】:

    标签: list swiftui uisearchbar


    【解决方案1】:

    只需删除多余的NavigationView - 在同一视图层次结构中只需要一个,显然父视图中已经有一些

    struct RecipeIngredientsView: View {
        let myArray = ["Dennis", "Tessa", "Peter", "Anna", "Tessa", "Klaus", "Xyan", "Zuhau", "Clown", "Brot", "Bauer"]
        @State private var searchText = ""
        @State private var showCancelButton: Bool = false
        
        var body: some View {
            NavigationView         // << remove this one !!
            {
    
    

    【讨论】:

    • 谢谢,这很容易.. xD
    猜你喜欢
    • 2020-12-05
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2015-05-01
    相关资源
    最近更新 更多