Created
September 25, 2020 02:44
-
-
Save yujinqiu/8b68b666739a19163a1f5264efd5d378 to your computer and use it in GitHub Desktop.
mac os SearchView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension NSTextField { | |
open override var focusRingType: NSFocusRingType { | |
get { .none } | |
set { } | |
} | |
} | |
struct SearchView: View { | |
@State var searchText: String = "" | |
@State private var hasFocus = false | |
#if !os(macOS) | |
private var backgroundColor = Color(UIColor.secondarySystemBackground) | |
#else | |
private var backgroundColor = Color(NSColor.controlBackgroundColor) | |
#endif | |
public var body: some View { | |
VStack { | |
HStack { | |
Spacer() | |
Image(systemName: "magnifyingglass") | |
TextField("Search", text: self.$searchText, onEditingChanged: { currentlyEditing in | |
}) | |
.textFieldStyle(PlainTextFieldStyle()) | |
.foregroundColor(.primary) | |
.padding(8) | |
.onTapGesture(count: 1, perform: { | |
self.hasFocus = true // If the editing state has changed to be currently edited, update the view's state | |
}) | |
Spacer() | |
} | |
.foregroundColor(.secondary) | |
.background(backgroundColor) | |
.cornerRadius(12) | |
.overlay(RoundedRectangle(cornerRadius: 12).stroke(self.hasFocus ? Color.accentColor : Color.clear, lineWidth: self.hasFocus ? 3: 0)) | |
.padding() | |
.frame(minWidth:0, idealWidth: 200, maxWidth: .infinity, minHeight: 0, idealHeight: 200, maxHeight: .infinity, alignment: .center) | |
} | |
Button(action: { | |
self.hasFocus = false | |
print("hi") | |
}, label: { | |
Text("Button") | |
}) | |
} | |
} | |
struct SearchView_Previews: PreviewProvider { | |
static var previews: some View { | |
SearchView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment