【问题标题】:SwiftUI - Capsule fill till StrokeBorderSwiftUI - 胶囊填充直到 StrokeBorder
【发布时间】:2021-12-06 11:09:45
【问题描述】:

我正在尝试填充封装的 strokeBorder,但不幸的是它正在填充矩形。见下图:

有什么想法可以让它只填充到笔画边框吗?我想将该字段用作搜索框,这就是我想在其中使用文本字段的原因。

这是我的代码:

struct ContentView: View {
    
    var body: some View {
        
        HStack{
            TextField("Search", text: .constant(""))
        }
        .padding()
        .frame(maxWidth:300)
        .background(
            Capsule()
                .strokeBorder(Color.black,lineWidth: 0.8)
                .background(Color.blue)
                .clipped()
        )
    }
}

非常感谢!

【问题讨论】:

标签: swiftui background border


【解决方案1】:

我想这就是你所追求的。

struct ContentView: View {

var body: some View {
    
    HStack{
        TextField("Search", text: .constant(""))
    }
    .padding()
    .frame(maxWidth:300)
    .background(
        Capsule()
            .strokeBorder(Color.black,lineWidth: 0.8)
            .background(Color.blue)
            .clipped()
    )
    .clipShape(Capsule())
    }
}

Xcode preview of HStack with capsule border

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
  • 很好的答案。小事,我注意到从 background 内部删除 .clipped() 并没有什么不同。
猜你喜欢
  • 2017-11-03
  • 2014-01-03
  • 2012-01-25
  • 2015-07-08
  • 2020-01-01
  • 2021-04-24
  • 2015-12-29
  • 1970-01-01
  • 2019-03-17
相关资源
最近更新 更多