UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 61- TextFields being cut off on DetailsView

Forums > 100 Days of SwiftUI

I'm on the finish line of day 61 challenge but I have a problem with DetailView. Both address and about TextFields are being cut off. I tried to fix it with layoutPriority(1) but than the list with all the friends disappeared or is cut off. Is there a way to add something like layoutPriority to both VStack with all the information and list of all the friends? Adding it to both doesn't work so I was wondering if there is another better way to do this. Here's my code for the DetailView:

import CoreData
import SwiftUI

struct DetailView: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: CDUser.entity(), sortDescriptors: []) var users: FetchedResults<CDUser>

    let user: CDUser
    @State private var friends: [CDUser] = []

    var body: some View {
        ScrollView(.vertical) {
            VStack {
                Text(user.wrappedName)
                    .font(.headline)
                    .padding()

                VStack(alignment: .leading) {
                    Text("Company: \(user.wrappedCompany)")
                    Text("Age: \(user.wrappedAge)")
                    Text("Email: \(user.wrappedEmail)")
                    Text("Address: \(user.wrappedAddress)")
                }.padding()

                Text(user.wrappedAbout)
                    .padding()
                    .layoutPriority(1)
            }

            if !user.friendsArray.isEmpty {
                VStack {
                    Text("Friends: ")
                        .font(.headline)

                    List(friends, id: \.id) { friend in
                        NavigationLink(destination: DetailView(user: friend)) {
                            VStack(alignment: .leading) {
                                Text(friend.wrappedName)
                                    .font(.headline)
                                    .foregroundColor(.secondary)
                                    .buttonStyle(PlainButtonStyle())
                            }
                        }
                    }
                }
            }
        }
        .navigationBarTitle(Text("Friends details"), displayMode: .inline)
        .onAppear(perform: findFriends)
    }

    func findFriends() {
        var matches = [CDUser]()
        for friend in self.user.friendsArray {
            if let match = self.users.first(where: { $0.id == friend.id }) {
                matches.append(match)
            } else {
                fatalError("Couldn't find a matching user \(friend)")
            }
            self.friends = matches
        }
    }
}

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.