【问题标题】:How to initiate a view with the ScrollView in the bottom in Swift ui?如何在 Swift ui 底部使用 ScrollView 启动视图?
【发布时间】:2019-07-08 14:06:11
【问题描述】:

我正在 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)
   }
  }
 }
}

我怎样才能将 ScrollView 默认放在底部?

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    由于ScrollView 不支持程序化滚动(但?曾经?),我对此的解决方案是旋转和翻转它。

    ScrollView{
        VStack{
            ForEach(items ) { item in
                Text(item.text)
                  .rotationEffect(.pi) // rotate each item
                  .scaleEffect(x: -1, y: 1, anchor: .center) // and flip it so we can flip the container to keep the scroll indicators on the right
            }
        }
    }
    .rotationEffect(.pi) // rotate the whole ScrollView 180º
    .scaleEffect(x: -1, y: 1, anchor: .center) // flip it so the indicator is on the right 
    

    【讨论】:

    • iOS 14 将支持程序化滚动
    • 值得注意的是,虽然这可行,但并不理想。如果在工作表中使用(例如)会出现问题,向上滑动超出 ScrollView 的内容会将工作表拉下以关闭,因为它认为这是视图的顶部(实际上是底部)。
    猜你喜欢
    • 2019-08-06
    • 2016-04-26
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2016-07-10
    相关资源
    最近更新 更多