我正在 Swift UI 中构建一个聊天应用程序,当我在“ChatView”中时,我想看到最后写入的消息,因此当消息位于屏幕上(FinalView)时,我需要将视图的滚动放在底部。
这是我正在使用的代码:
ScrollView {
ForEach(self.userController.userMessage.identified(by: \.self)) { message in
if message.from == UserService.currentUserID! {
HStack {
Spacer()
Text(message.message)
.bold()
.foregroundColor(Color.white)
.padding(8)
.background(Color.blue, cornerRadius: 8)
}
} else {
HStack {
Text(message.message)
.bold()
.foregroundColor(Color.white)
.padding(8)
.background(Color.gray, cornerRadius: 8)
Spacer()
}
.offset(y: 8)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何默认将 ScrollView 始终放在底部?
从那时起我对此的解决方案ScrollView是旋转和翻转它。
ScrollView{\n VStack{\n ForEach(items ) { item in\n Text(item.text)\n .rotationEffect(.pi) // rotate each item\n .scaleEffect(x: -1, y: 1, anchor: .center) // and flip it so we can flip the container to keep the scroll indicators on the right\n }\n }\n}\n.rotationEffect(.pi) // rotate the whole ScrollView 180\xc2\xba\n.scaleEffect(x: -1, y: 1, anchor: .center) // flip it so the indicator is on the right \nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
935 次 |
| 最近记录: |