简体   繁体   English

NavigationLink 不适用于第一个元素

[英]NavigationLink not working on first element

I am using searchable to display users matching the name searched.我正在使用 searchable 来显示与搜索的名称匹配的用户。

What's happening is if I have names in similar searches (ie. Sarah & Sara) and those two names are displayed at the same time, the first navigationLink is not clickable.发生的情况是,如果我在类似搜索中有名称(即 Sarah 和 Sara)并且这两个名称同时显示,则第一个 navigationLink 是不可点击的。 I'll post a screenshot below as an example.我将在下面发布一个屏幕截图作为示例。 Meanwhile the second, third, etc works fine.同时,第二个,第三个等工作正常。

(As a note, I don't think my mouse cursor showed up on the GIF, but I was clicking the first button and no results). (作为说明,我认为我的鼠标 cursor 没有出现在 GIF 上,但我点击了第一个按钮,但没有结果)。

struct FollowingListRow: View {

    var body: some View {
        HStack{
            Image("exampleImage")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width:60, height: 60)
                .clipShape(Circle())
                .padding(.trailing, 45)
             
            VStack{
                Text("Test")
                    .font(.title2)
                NavigationLink(destination: TestView()){
                    Text("View Profile")
                    .font(.caption)
                    .foregroundColor(.black)
                    .padding(3) //general padding
                    .padding([.leading, .trailing], 15) // << side padding
                    .border(.black)
                    .padding(.top, -5) // <<bring up button
                }
               
              
            }
            
        }
        .padding()
    }
}

struct FollowingListView: View {
@State var allUsers = ["Sara", "Sarah", "Teddy", "Jill"]
@State private var userSearch = ""

var body: some View {
    NavigationView{
            VStack{
                //DISPLAY USERS
                ScrollView{
                    ForEach ((allUsers), id:\.id ) { user in
                        if user.name.contains(userSearch){
                            FollowingListRow()
                        }
                    }
                }
            }
            .padding(.top, 15) // << add separation from list and search bar
            .navigationBarTitle("Search Users")
           
        //SEARCH
            .searchable(text: $userSearch,placement: .navigationBarDrawer(displayMode: .always), prompt: "Search for Users") // << always display search bar
    }

    
}

在此处输入图像描述

Demo演示

   struct FollowingListRow: View {
    
    //MARK: - PROPERTIES
    var name: String
    
    //MARK: - BODY
    var body: some View {
        
        HStack{
            Image("exampleImage")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width:60, height: 60)
                .clipShape(Circle())
                .padding(.trailing, 45)
            
            VStack{
                Text(name)
                    .font(.title2)
                NavigationLink(destination: Text("\(name)")){
                    Text("View Profile")
                        .font(.caption)
                        .foregroundColor(.black)
                        .padding(3) //general padding
                        .padding([.leading, .trailing], 15) // << side padding
                        .border(.black)
                        .padding(.top, -5) // <<bring up button
                }
                
            }
            
        }
        .padding()
    }
}

struct FollowingListView: View {
    
    @State private var allUsers: [User] = [
        User(name: "Sara", id: 1),
        User(name: "Sarah", id: 2),
        User(name: "Teddy", id: 3),
        User(name: "Jill", id: 4)
    ]
    
    @State private var userSearch = ""
    
    var body: some View {
        NavigationView{
            VStack{
                //DISPLAY USERS
                ScrollView{
                    ForEach (allUsers, id:\.id ) { user in
                        if user.name.contains(userSearch){
                            FollowingListRow(name: user.name)
                        }
                    }
                }
            }
            .padding(.top, 15) // << add separation from list and search bar
            .navigationBarTitle("Search Users")
            
            //SEARCH
            .searchable(text: $userSearch,placement: .navigationBarDrawer(displayMode: .always), prompt: "Search for Users") // << always display search bar
        }
        
        
    }
}


//User Model
struct User{
    var name: String
    var id: Int
}

I have modified your code a bit with dummy data for testing purpose.为了测试目的,我用虚拟数据修改了你的代码。 But the navigations are working fine.但导航工作正常。 Please check your Destination TestView.The issue could be there.请检查您的目标测试视图。问题可能在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM