我的视图中有一个 Select 器,我不知道如何访问边框 colored颜色 .这是代码:

Picker("Select a Project", selection: $selectedProjectString){
            ForEach(projects, id: \.self) {
                Text($0).lineLimit(1).frame(height:120)
            }
        }.foregroundColor(Color.white)
        .frame(height: 120)

Select 器看起来像这样,我希望绿色边框也是白色的.

enter image description here

谢谢

更新:

enter image description here

推荐答案

您可以通过自定义扩展来实现这一点.您可以将边框 colored颜色 设置为任何 colored颜色 .(代码在图片下方)

贷记到@Tamas

enter image description here

测试视图:

Picker("Select a Project", selection: $pick){
                ForEach(animal, id: \.self) {
                    Text($0).lineLimit(1).frame(height:120)
                }
    }
    .focusBorderColor(color: .red)
    .foregroundColor(Color.white)
    .frame(height: 120)

扩展名:

extension Picker {
func focusBorderColor(color: Color) -> some View {
    let isWatchOS7: Bool = {
        if #available(watchOS 7, *) {
            return true
        }
        return false
    }()

    let padding: EdgeInsets = {
        if isWatchOS7 {
            return .init(top: 17, leading: 0, bottom: 0, trailing: 0)
        }
        return .init(top: 8.5, leading: 0.5, bottom: 8.5, trailing: 0.5)
    }()

    return self
        .overlay(
            RoundedRectangle(cornerRadius: isWatchOS7 ? 8 : 7)
                .stroke(color, lineWidth: isWatchOS7 ? 4 : 3.5)
                .offset(y: isWatchOS7 ? 0 : 8)
                .padding(padding)
        )
  }
}

Swift相关问答推荐

操作员如何点逻辑非(.!)在Swift工作?

如何在SWIFT中使用DiscardingTaskGroup获得任务结果

如何在visionOS中定制悬停效果区域

将`@Environment`值赋给`Binding`参数

为SwiftUI文本视图提供固定宽度,并仅在文本换行时使文本视图底部增大

在不传递参数的情况下使用Init方法?

有没有办法让文本字段以不同的 colored颜色 显示而不影响半透明背景?

覆盖一个子元素的 HStack 对齐方式

如何让飞机与 RealityKit 中的物体相撞

从 iPhone 中的安全飞地获取真正的随机数?

iOS16 Bug 键盘在表单解雇 SwiftUI 时打破布局

如何在 SwiftUI 中获取视图的位置

在 SwiftUI 中操作绑定变量

如何在 SwiftUI 中正确实现Collection 夹?

在 xcode 13 中的构建之间保持可访问性权限

在 Swift 4 中,如何删除基于块的 KVO 观察者?

UITableViewCell 不显示 detailTextLabel.text - Swift

快速在 LLDB 中使用 po

Swift 3:日期与 NSDate?

如何发送静默推送通知有效负载