[英]In SwiftUI how do I set the environment variable of editMode in an XcodePreview
Given a navigation stack hierarchy like this where the edit button is added in another source file 给定这样的导航堆栈层次结构,其中将编辑按钮添加到另一个源文件中
struct ContentView : View {
var body: some View {
NavigationView {
EditingView()
}.navigationBarItems(trailing: EditButton())
}
}
And the view that uses the edit button is elsewhere in the codebase 使用“编辑”按钮的视图位于代码库的其他位置
struct EditingView : View {
@State var dataValue: String = "Initial Value"
@Environment(\.editMode) var editMode
var body: some View {
VStack(alignment: .leading) {
if self.editMode?.value == .inactive {
Text(dataValue)
} else {
TextField(($dataValue))
}
}.padding()
.navigationBarTitle("Lets edit some State")
.navigationBarItems(trailing: EditButton())
}
}
Can I programmatically set the initial editing mode in the preview? 我可以在预览中以编程方式设置初始编辑模式吗? Is there a way to view the EditingView without an editing button using the environment?
有没有一种方法可以使用环境在没有编辑按钮的情况下查看EditingView? A few ways I have found that work are shown in the snippet but I would hope that I can find a method to set and initial value programmatically with the environment.
片段中显示了一些我可以找到工作的方法,但是我希望我能找到一种方法来通过编程环境设置和初始值。
#if DEBUG
struct EditingView_Previews : PreviewProvider {
static var previews: some View {
NavigationView {
VStack {
EditingView()
// I would prefer to use an environment variable.
// Here is the best thought at some code:
//
// `.environment(\.editMode, .inactive)`
//
// the compiler Error returned:
// Type 'Binding<EditMode>?' has no member 'inactive'
//
// which I understand since it is a binding
// not a direct access to an enum variable.
// But can I set it somehow or should I let the system
// handle it entirely?
// So to get around the issue I have an extra edit button
EditButton()
}
}
}
// Or this could work
//.navigationBarItems(trailing: EditButton())
}
#endif
An Example project can be found here: https://github.com/PaulWoodIII/PreviewEditMode 可以在这里找到示例项目: https : //github.com/PaulWoodIII/PreviewEditMode
The environment variable editMode
is a binding. 环境变量
editMode
是一个绑定。 You can set programmatically for example as follows: 您可以通过编程方式进行如下设置:
struct outerView: View {
@State var editMode: EditMode = .active
var body: some View {
return InnerView(model: model).environment(\.editMode, $editMode)
}
}
You can pass this in as a constant, by adding to your preview provider: 您可以通过添加到预览提供程序中,将其作为常量传递:
.environment(\.editMode, Binding.constant(EditMode.active))
eg: 例如:
struct EditingView_Previews : PreviewProvider {
static var previews: some View {
NavigationView {
EditingView()
}
}
.environment(\.editMode, Binding.constant(EditMode.active))
}
}
I found the fact that 'EditMode' does not work when there is a NavigationView
, but I don't find any document refer to that. 我发现存在
NavigationView
时'EditMode'不起作用的事实,但是我找不到任何文档引用该事实。 The following code works well 以下代码效果很好
struct ContentView: View {
@Environment(\.editMode) var mode
var body: some View {
HStack {
if self.mode?.wrappedValue == .active {
Button("Cancel") {
self.mode?.animation().wrappedValue = .inactive
}
}
Spacer()
EditButton()
}
}
}
But it does not work when there's NavigationView 但是当有NavigationView时它不起作用
struct ContentView: View {
@Environment(\.editMode) var mode
var body: some View {
NavigationView {
HStack {
if self.mode?.wrappedValue == .active {
Button("Cancel") {
self.mode?.animation().wrappedValue = .inactive
}
}
Spacer()
EditButton()
}
}
}
}
This doesn't solve the problem of being able to set the environment value of EditMode
, which I would also love to know how to do, but a slightly nicer workaround is to hide an EditButton
behind the view you are previewing: 这不能解决能够设置
EditMode
的环境值的问题,我也很想知道该怎么做,但是一个更好的解决方法是将EditButton
隐藏在要预览的视图后面:
struct ProfileHost_Previews : PreviewProvider {
static var previews: some View {
ZStack {
EditButton().hidden()
ProfileHost()
}
}
}
assuming the view you are previewing already has a method to toggle the edit mode, such as an EditButton
. 假设您正在预览的视图已经具有切换编辑模式的方法,例如
EditButton
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.