Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在SwiftUI中的形状上添加多个文本覆盖_Ios_Swiftui_Overlay_Shapes - Fatal编程技术网

Ios 在SwiftUI中的形状上添加多个文本覆盖

Ios 在SwiftUI中的形状上添加多个文本覆盖,ios,swiftui,overlay,shapes,Ios,Swiftui,Overlay,Shapes,我正在做一个项目,我在SwiftUI中使用不同的形状,例如,这个圆角矩形我想通过叠加在上面添加文本,但它不允许我添加多行。我试图搜索一些覆盖的教程,但总是找到一个只显示一个文本生命的例子 所以我想知道是否有一种方法可以在这样的形状上添加多行文本覆盖,或者是否有更好的方法来实现它。 谢谢你的反馈 以下是当前代码: import SwiftUI struct ContentView: View { var body: some View { HStack

我正在做一个项目,我在SwiftUI中使用不同的形状,例如,这个圆角矩形我想通过叠加在上面添加文本,但它不允许我添加多行。我试图搜索一些覆盖的教程,但总是找到一个只显示一个文本生命的例子

所以我想知道是否有一种方法可以在这样的形状上添加多行文本覆盖,或者是否有更好的方法来实现它。

谢谢你的反馈

以下是当前代码:

import SwiftUI


struct ContentView: View {
    var body: some View {
        
        HStack {
        RoundedRectangle(cornerRadius: 20)
            .frame(width: 320, height: 200)
            .foregroundColor(Color.blue)
            .overlay(
                Text("Hello there")
           )
            
      }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
这是我理想中想要的,但这种方式不行:

import SwiftUI


struct ContentView: View {
    var body: some View {
        
        HStack {
        RoundedRectangle(cornerRadius: 20)
            .frame(width: 320, height: 200)
            .foregroundColor(Color.blue)
            .overlay(
                Text("Hello there")
                Text("Hello there")
                Text("Hello there")
                Text("Hello there")
                Text("Hello there")
                Text("Hello there")
                
           )
            
      }
    }
}

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


将文本与
VStack
HStack
等任意一种包装在一个容器中

struct ContentView: View {
    var body: some View {
           HStack {
           RoundedRectangle(cornerRadius: 20)
               .frame(width: 320, height: 200)
               .foregroundColor(Color.blue)
               .overlay(
                Group{ //<-- Here
                   Text("Hello there")
                   Text("Hello there")
                   Text("Hello there")
                   Text("Hello there")
                   Text("Hello there")
                   Text("Hello there")
                }
              )
         }
       }
}
struct ContentView: View {
    var body: some View {
           HStack {
           RoundedRectangle(cornerRadius: 20)
               .frame(width: 320, height: 200)
               .foregroundColor(Color.blue)
               .overlay(
                overlayView
              )
         }
       }
    
    // Make your view here
    @ViewBuilder
    private var overlayView: some View {
        Text("Hello there")
        Text("Hello there")
        Text("Hello there")
        Text("Hello there")
        Text("Hello there")
        Text("Hello there")
    }
}