ios - 具有 swiftui 形状的自适应框架(小部件)大小
问题描述
我想调整小部件的大小以适应小部件内容的整体大小。问题是,标准形状(小部件的背景)默认为可用空间,我不想硬编码它的框架。有什么建议,例如使用 GeometryReader?
struct WidgetView: View {
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 40, style: .continuous)
.foregroundColor(.gray)
Text("TestingText")
}
}
}
解决方案
我想你想要这个
struct WidgetView: View {
var body: some View {
Text("TestingText")
.background(RoundedRectangle(cornerRadius: 40, style: .continuous)
.foregroundColor(.gray))
}
}
更新:我认为您的担忧是由于较大的拐角半径可能会削减一些内部内容。
这是一些方法
struct WidgetView<V: View>: View { // more generic view
var content: () -> V
var body: some View {
content()
.background(RoundedRectangle(cornerRadius: 40, style: .continuous)
.foregroundColor(.gray).padding(-20)) // compensation !!
}
}
测试(Xcode 11.4 / iOS 13.4)
WidgetView {
VStack {
Image(systemName: "car")
.resizable().frame(width: 80, height: 80)
Text("Some Label")
}
}
输出
推荐阅读
- visual-c++ - 来自按钮单击处理程序的 MFC 中的弹出菜单不起作用
- c++ - 如何在创建/销毁之前/之后检查 cudastream 的状态?
- python - 在 Windows 上使用 / 作为路径,并让 stdlib 函数也使用它(glob、os.path 等)
- hybris - 辅助服务模块未在 Spartacus B2B 中与 SAP Commerce cloud 2005/2011 (Hybris) 一起运行
- javascript - 在 Typescript 中为现有类型扩展并添加新属性
- sql-server - VBA Excel - SQL“执行”方法触发查询超时过期
- c# - Sqlite 批量插入新事务
- php - Json 用特殊字符编码
- ffmpeg - 使用特定的叠加起始位置从左到右移动叠加
- php - 返回 eloquent 模型关系时获取空值