我尝试重新实现我正在使用的 SegmentedControlers,因为它们在 Xcode 11 beta 5 中已被弃用。这花了一段时间但我得到了我想要的外观。但是,当我用 onTapGesture() 替换 tapAction 时,选择器停止工作。
下面的代码显示了问题。注释掉 pickerStyle 会得到一个与 onTapGesture() 一起工作的轮选择器
import SwiftUI
var oneSelected = false
struct ContentView: View {
@State var sel = 0
var body: some View {
VStack {
Picker("Test", selection: $sel) {
Text("A").tag(0)
Text("B").tag(1)
Text("C").tag(2)
}
.pickerStyle(SegmentedPickerStyle())
Picker("Test", selection: $sel) {
Text("A").tag(0)
Text("B").tag(1)
Text("C").tag(2)
}
.pickerStyle(SegmentedPickerStyle())
.onTapGesture(perform: {
oneSelected = (self.sel == 1)
})
Text("Selected: \(sel)")
}
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个将 Core Data 与 SwiftUI 相结合的应用程序。一切都很顺利,直到我将更新代码放入工作表中,之后我得到了一个“Foundation._GenericObjCError”错误 0 我相信这意味着没有错误,但我的持久容器仍然没有得到更新。
这种组合是一个已知问题吗?
我用来将条目添加到我的 Store 条目的代码:
struct StoreAdd: View {
@Environment(\.managedObjectContext)
var managedObjectContext
@State
var name = ""
@State
var branch = ""
var body: some View {
VStack {
TextField("Store name", text: $name)
TextField("Store branch", text: $branch)
Button(
action: {
let store = Store(context: self.managedObjectContext)
store.id = UUID()
store.name = self.name
store.branch = self.branch.isEmpty ? nil : self.branch
self.managedObjectContext.persist()
},
label: { Text("Add")}
)
.disabled(name.isEmpty)
}
.padding(.horizontal, 20)
}
}
Run Code Online (Sandbox Code Playgroud)
persist()
是围绕save() …
将按钮向上移动到导航栏中仅当附加了工作表时才会触发一次。该按钮用于显示搜索窗口,但弹出窗口关闭后该按钮将保持非活动状态。
所附代码是我尝试过的简化版本。起初,我在主要部分使用了一个按钮来激活搜索窗口,但我认为导航栏会占用更少的空间。激活有效,但在这种情况下我无法停用它。
import SwiftUI
struct ContentView: View {
@State var showingSearch: Bool = false
var body: some View {
NavigationView {
VStack {
Text("Hello World!")
Button(
action: { self.showingSearch = true },
label: { Image(systemName: "magnifyingglass") }
)
.sheet(
isPresented: $showingSearch,
content: { Search( showingSearch: self.$showingSearch ) }
)
}
.navigationBarItems(
leading: Image(systemName: "square.and.pencil"),
trailing: Button(
action: { self.showingSearch = true },
label: { Image(systemName: "magnifyingglass") }
)
.sheet(
isPresented: $showingSearch,
content: { Search( showingSearch: self.$showingSearch ) }
)
) …
Run Code Online (Sandbox Code Playgroud) 我尝试在iPhone XR模拟器上横向运行我的应用程序,但出现黑屏。
下面的代码是我的测试。如果删除NavigationView,它可以在iPhone 8模拟器上正常运行,也不能在iPhone XR模拟器上正常运行。
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
GeometryReader { gp in
VStack(alignment: HorizontalAlignment.center) {
Text("Width: \(gp.size.width)")
Text("Height: \(gp.size.height)")
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望我会同时看到横向和纵向的屏幕尺寸。
有人对这种组合有经验吗?