我正在尝试重新创建 Apple 的消息应用程序,我注意到聊天选择上的 V 形位于顶部,而不是像 navigationLink 默认提供的那样位于中心。
以下是 Apple Messages 的图片:
这是我的应用程序的图片:
import SwiftUI
struct ContentView: View {
private var imageSize: CGFloat = 50
var body: some View {
NavigationStack {
List(NavigationScreens.allCases, id: \.self) { screen in
NavigationLink(value: screen) {
Label(title: {
MessageChat(name: "SwiftUI")
.padding(.leading, 15)
//.border(.red)
}, icon: {
HStack {
Image(systemName: "circle.fill")
.resizable()
.frame(width: 10, height: 10)
Image("image_test")
.resizable()
.frame(width: imageSize, height: imageSize)
.clipShape(Circle())
}
.padding(10)
})
}
}
.navigationTitle("Messages")
.listStyle(.plain)
.scrollContentBackground(.hidden)
.navigationDestination(for: NavigationScreens.self) { screen in
switch screen {
case .Chat, .Profile, .Settings:
ChatSelection()
}
}
}
}
}
我不完全确定这是否是正确的做法或者是否需要自定义的 NavigationLink。
留言聊天代码:
struct MessageChat: View {
@State var contact: Contact
var body: some View {
HStack {
VStack(alignment: .leading, spacing: 4) {
HStack {
// MARK: Name
Text(contact.name)
.font(.headline)
.lineLimit(1)
.foregroundStyle(Color.primary)
Spacer()
// MARK: Message Date
Text("12/12/12")
.font(.subheadline)
.foregroundStyle(Color.gray)
}
.padding(.trailing, 20)
// MARK: Message Description
Text("Some random text long enough to have the multiple lines effect, just in case its not long enough")
.lineLimit(2)
.font(.subheadline)
.foregroundStyle(Color.secondary)
.frame(maxHeight: 40, alignment: .topLeading)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}