【问题标题】:VStack is not fitting in ContentView SwiftUIVStack 不适合 ContentView SwiftUI
【发布时间】:2019-10-08 10:45:27
【问题描述】:

我用 SwiftUI 创建了一个应用 我已经使用此代码来布局我的视图

struct ContentView: View {
    var body: some View {
        List(0 ..< 5) { item in
            VStack {
                Text("Hello World")
                    .font(.largeTitle)
                    .fontWeight(.bold)
                    .foregroundColor(Color.red)
                Image("Hello")
                    .aspectRatio(contentMode: .fit)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

但是 VStack 不适合 contentView

【问题讨论】:

    标签: ios swiftui xcode11


    【解决方案1】:

    只需将 .resizable() 添加到您的图像中。

    【讨论】:

      【解决方案2】:

      在其周围包裹一个 ScrollView{}。

      struct ContentView: View {
      var body: some View {
          List(0 ..< 5) { item in
            ScrollView{
              VStack {
                  Text("Hello World")
                      .font(.largeTitle)
                      .fontWeight(.bold)
                      .foregroundColor(Color.red)
                  Image("Hello")
                      .aspectRatio(contentMode: .fit)
              }
            }
          }
        }
      }
      
      struct ContentView_Previews: PreviewProvider {
          static var previews: some View {
              ContentView()
          }
      }
      

      【讨论】:

      • 能否用完整的代码解释一下?我不明白
      • 您还必须按右下角的播放按钮。或者在模拟器上运行它,让滚动工作
      • VStack 不需要 ScrollView
      • 我不明白你的目标是什么?您的内容太大,因此您必须创建一个滚动视图,以便您可以滚动您的内容
      • aspectRatio 修饰符的使用意味着希望缩放图像以适应,但aspectRatio 修饰符(本身)不会使Image 可调整大小。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 2021-02-18
      • 2020-05-16
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多