如何在swiftUI中创建普通视图 [英] How to create a plain view in swiftUI

查看:54
本文介绍了如何在swiftUI中创建普通视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 SwiftUI 中创建具有背景颜色的纯视图.但是我能找到的所有内容都不是纯文本形式的元素,例如文本",按钮",图像",列表"等.

I'm trying to create a plain view with a background color in SwiftUI. But everything I can find are elements that are not plain views like Text, Button, Image, List, etc..

当我尝试使用 View 时,它显示以下错误消息:

When I try to use View, it shows me following error messages:

  • 无法构建视图",因为它没有可访问的初始化器
  • 查看"协议只能用作一般约束,因为它具有Self或associatedType要求

如何创建具有背景色的矩形视图?

How do I create a rectangular view with background color?

推荐答案

只需使用 Rectangle()

文档指出:

在包含矩形的视图框架内对齐的矩形.

A rectangular shape aligned inside the frame of the view containing it.

下面是一个具有固定大小和背景颜色的矩形的示例

Here an example of a rectangle with fixed size and background color

Rectangle()
    .size(CGSize(width: 10, height: 10))
    .foregroundColor(.red)

这篇关于如何在swiftUI中创建普通视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆