如何在Swiftui中创建自定义形状

发布于 2025-02-02 16:08:36 字数 541 浏览 2 评论 0原文

我正在学习Swiftui,我想在应用程序中创建一些形状。

我没有重复代码,而是决定创建一个函数以显示形状。

我试图创建一个包含形状的函数,并将其称为我需要的位置。

import SwiftUI

struct HomeView: View {
    var body: some View {
        VStack{
            HStack{
               rect()
            }
        }
    }
}

func rect() {
    Rectangle()
        .frame(width: 100, height: 100)
        .cornerRadius(18)
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}

我的问题是什么,我该如何修复它,以便可以为按钮,文本框,图像等创建不同的样式...

I'm learning swiftui and I want to create some shapes in my app.

I instead of repeating my code I decided to create a function to display the shape.

I tried to create a func that contains the shape and call it where I need it.

import SwiftUI

struct HomeView: View {
    var body: some View {
        VStack{
            HStack{
               rect()
            }
        }
    }
}

func rect() {
    Rectangle()
        .frame(width: 100, height: 100)
        .cornerRadius(18)
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}

What is my problem here, and how can I fix it so I can create different styles for buttons, textboxes, images, etc...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虐人心 2025-02-09 16:08:36

您在正确的轨道上,而不是定义函数,而是定义一个新视图:

struct MyRect: View {

    var body: some View {
        Rectangle()
            .frame(width: 100, height: 100)
            .cornerRadius(18)
    }
}

// Embed with 'MyRect()'

或者,代码中的func rect可以具有返回类型某些视图

func rect() -> some View {
    Rectangle()
        .frame(width: 100, height: 100)
        .cornerRadius(18)
}

// Embed with 'rect()'

You're on the right track, instead of defining a function, define a new view:

struct MyRect: View {

    var body: some View {
        Rectangle()
            .frame(width: 100, height: 100)
            .cornerRadius(18)
    }
}

// Embed with 'MyRect()'

Alternatively, the func rect in your code could have a return type some View:

func rect() -> some View {
    Rectangle()
        .frame(width: 100, height: 100)
        .cornerRadius(18)
}

// Embed with 'rect()'
醉梦枕江山 2025-02-09 16:08:36

要创建一个函数以显示视图,必须创建@ViewBuilder函数并返回一些视图。

您需要做这样的事情: -

      @ViewBuilder func rect() -> some View {
   
              Rectangle()
                  .frame(width: 100, height: 100)
                  .cornerRadius(18)

      }

To create a function for showing the view one must create @ViewBuilder function and return some View.

You need to do something like this :-

      @ViewBuilder func rect() -> some View {
   
              Rectangle()
                  .frame(width: 100, height: 100)
                  .cornerRadius(18)

      }
沩ん囻菔务 2025-02-09 16:08:36

func rect是类型voidview> view返回某些视图中的正文。您应该使函数的返回类型符合view,所以要么:

  1. 返回形状func rect() - >形状{

  2. 返回视图func rect() - >一些查看{

请注意,如果使用选项号1,则无法返回任何不是shape的东西。

The func rect is of type Void, the body in your View returns some View. You should make your function's return type conform to View, so either:

  1. return a Shape func rect() -> Shape {

  2. return a View func rect() -> some View {

Do note that if go with option number 1 you cannot return anything that is not a Shape.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文