

我怎樣才能獲得類似于您在影像中看到的效果,就像在 xcode 上一樣。
還要考慮淺色和深色主題的可能性。
import SwiftUI
struct FindNavigatorSearchBar: View {
@ObservedObject
var state: WorkspaceDocument.SearchState
let title: String
@Binding
var text: String
@FocusState
private var focusState: Bool
var body: some View {
HStack {
Image(systemName: "magnifyingglass")
.foregroundColor(Color(nsColor: .secondaryLabelColor))
textField
if !text.isEmpty { clearButton }
}
.padding(.horizontal, 5)
.padding(.vertical, 3)
.background(focusState ? .quaternary : .tertiary)
.overlay(RoundedRectangle(cornerRadius: 8).stroke(Color.gray, lineWidth: 0.5).cornerRadius(8))
.cornerRadius(8)
}
private var textField: some View {
TextField(title, text: $text)
.focused($focusState)
.disableAutocorrection(true)
.textFieldStyle(PlainTextFieldStyle())
}
private var clearButton: some View {
Button {
self.text = ""
state.search("")
} label: {
Image(systemName: "xmark.circle.fill")
}
.foregroundColor(.secondary)
.buttonStyle(PlainButtonStyle())
}
}
struct SearchBar_Previews: PreviewProvider {
static var previews: some View {
HStack {
FindNavigatorSearchBar(
state: .init(WorkspaceDocument.init()),
title: "placeholder",
text: .constant("value")
)
}
.padding()
}
}
uj5u.com熱心網友回復:
可以使用具有焦點依賴狀態的背景,例如
.background(RoundedRectangle(cornerRadius: 4)
.stroke(Color.gray, lineWidth: 0.5).cornerRadius(4)
.background(focused ? .black : .clear) // << here !!
.clipShape(RoundedRectangle(cornerRadius: 4))
)

完整的測驗模塊在這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/465184.html
