Tips 提示 #15
Replies: 9 comments
-
具有可选值的选择器
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
将一个视图作为另一个视图的内容传递struct WrapperView<ContentView: View>: View {
let content: ContentView
init(@ViewBuilder content: () -> ContentView) {
self.content = content()
}
var body: some View {
// render here children View
content
}
} 使用 WrapperView() {
AnotherView()
} |
Beta Was this translation helpful? Give feedback.
-
三栏布局struct ContentView: View {
var body: some View {
NavigationView {
Text("Hello, world!")
Text("List")
.toolbar{
Text("List Toolbar")
}
Text("Content")
.toolbar{
Text("app")
Spacer()
Text("app")
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
三栏布局 - 三栏磨砂玻璃效果import SwiftUI
@main
struct DemoApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.windowToolbarStyle(UnifiedCompactWindowToolbarStyle())
.windowStyle(HiddenTitleBarWindowStyle())
}
}
struct ContentView: View {
var body: some View {
NavigationView {
List {
Text("Hello, world!")
}
.listStyle(SidebarListStyle())
List {
Text("List")
}
.listStyle(SidebarListStyle())
.toolbar{
Text("List Toolbar")
}
List {
Text("Content")
}
.listStyle(SidebarListStyle())
.toolbar{
Text("app")
Spacer()
Text("app")
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
搜索输入框 macOSimport SwiftUI
struct SearchField: NSViewRepresentable {
@Binding var search: String
class Coordinator: NSObject, NSSearchFieldDelegate {
var parent: SearchField
init(_ parent: SearchField) {
self.parent = parent
}
func controlTextDidChange(_ notification: Notification) {
guard let searchField = notification.object as? NSSearchField else {
print("Unexpected control in update notification")
return
}
self.parent.search = searchField.stringValue
}
}
func makeNSView(context: NSViewRepresentableContext<SearchField>) -> NSSearchField {
let tf = NSSearchField(frame: .zero)
tf.drawFocusRingMask()
return tf
}
func updateNSView(_ searchField: NSSearchField, context: NSViewRepresentableContext<SearchField>) {
searchField.stringValue = search
searchField.delegate = context.coordinator
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
}
struct ContentView: View {
@State private var text: String = ""
var body: some View {
let binding = Binding<String>(
get: { self.text },
set: {
self.text = $0;
}
)
return SearchField(search: binding)
}
} |
Beta Was this translation helpful? Give feedback.
-
显示全屏渐变背景import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello World")
}
}
@main
struct DemoApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.frame(width: 300, height: 120, alignment: .center)
.padding()
.background(
LinearGradient(gradient: Gradient(colors: [.red, .black]), startPoint: .top, endPoint: .bottom)
.ignoresSafeArea()
)
}
.windowStyle(HiddenTitleBarWindowStyle())
}
}
|
Beta Was this translation helpful? Give feedback.
-
修复点击区域不触发点击事件问题struct ContentView: View {
var body: some View {
HStack {
Text("Title:")
Spacer()
Text("Blub")
}
.border(Color.red)
.onTapGesture { print("Title tapped!") }
}
} 修复方法: struct ContentView: View {
var body: some View {
HStack {
Text("Title:")
Spacer()
Text("Blub")
}
.contentShape(Rectangle())
.border(Color.red)
.onTapGesture { print("Title tapped!") }
}
}
|
Beta Was this translation helpful? Give feedback.
-
使用 clipped 修饰符隐藏任何超出形状的布局范围的内容Text("This long text string is clipped")
.fixedSize()
.frame(width: 175, height: 100)
.clipped()
.border(Color.gray) 设置 没有设置 |
Beta Was this translation helpful? Give feedback.
-
分享一些
SwiftUI
快速提示 or 示例。附加准则:
使用
### Title
作为提示标题。检查语法。
确保您的示例代码可运行。
Beta Was this translation helpful? Give feedback.
All reactions