我正在尝试创建一个自定义的SwiftUI视图,该视图的行为类似于默认视图,在其中我可以使用方法或可选的初始化方法参数向视图中添加额外的内容。
SomeCustomView(title: "string argument") {
// some view
}
SomeCustomView(title: "hello") {
// some view
}.sideContent {
// another view
}
// This style is acceptable too
SomeCustomView(title: "hello", sideContent: { /* another view */ }) {
// some view
}
如何修改此视图结构以使其表现得与上述示例相同?
struct SomeCustomView<Content>: View where Content: View {
let title: String
let content: Content
init(title: String, @ViewBuilder content: () -> Content) {
self.title = title
self.content = content()
}
var body: some View {
VStack {
Text(title)
content
}
}
}
理想情况下,我将有两个不同的主体“模板”,可以根据是调用sideContent
方法还是设置sideContent
参数来在它们之间进行切换。例如,
var body: some View {
VStack {
Text(title)
content
}
}
// or
var otherBody: some View {
HStack {
VStack {
Text(title)
content
}
sideContent
}
}
答案 0 :(得分:3)
对于容器视图,我遵循的一种模式是使用条件扩展一致性来支持针对不同变体的初始化程序。
这是带有可选页脚的简单面板视图的示例。
struct Panel<Content: View, Footer: View>: View {
let content: Content
let footer: Footer?
init(@ViewBuilder content: () -> Content, footer: (() -> Footer)? = nil) {
self.content = content()
self.footer = footer?()
}
var body: some View {
VStack(spacing: 0) {
content
// Conditionally check if footer has a value, if desirable.
footer
}
}
}
// Support optional footer
extension Panel where Footer == EmptyView {
init(@ViewBuilder content: () -> Content) {
self.content = content()
self.footer = nil
}
}
我相信这与Apple支持内置类型的所有变体的方式类似。例如,这是Button
的标题的片段。
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension Button where Label == PrimitiveButtonStyleConfiguration.Label {
/// Creates an instance representing the configuration of a
/// `PrimitiveButtonStyle`.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public init(_ configuration: PrimitiveButtonStyleConfiguration)
}
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension Button where Label == Text {
/// Creates an instance with a `Text` label generated from a localized title
/// string.
///
/// - Parameters:
/// - titleKey: The key for the localized title of `self`, describing
/// its purpose.
/// - action: The action to perform when `self` is triggered.
public init(_ titleKey: LocalizedStringKey, action: @escaping () -> Void)
/// Creates an instance with a `Text` label generated from a title string.
///
/// - Parameters:
/// - title: The title of `self`, describing its purpose.
/// - action: The action to perform when `self` is triggered.
public init<S>(_ title: S, action: @escaping () -> Void) where S : StringProtocol
}
答案 1 :(得分:1)
经过一番思考和反复试验,我弄清楚了。事后看来,这似乎有些明显。
struct SomeCustomView<Content>: View where Content: View {
let title: String
let content: Content
init(title: String, @ViewBuilder content: @escaping () -> Content) {
self.title = title
self.content = content()
}
func sideContent<SideContent>(@ViewBuilder side: @escaping () -> SideContent) -> some View {
HStack {
body // body is just a View, so we can compose with this View
side()
}
}
var body: some View {
VStack {
Text(title)
content
}
}
}
无论有没有方法调用,它都可以工作。
SomeCustomView(title: "string argument") {
// some view
}
SomeCustomView(title: "hello") {
// some view
}.sideContent {
// another view
}
答案 2 :(得分:0)
我建议使用ViewModifyer
而不是自定义视图。这些工作如下:
struct SideContent<SideContent: View>: ViewModifier {
var title: String
var sideContent: (() -> SideContent)?
init(title: String) {
self.title = title
}
init(title: String, @ViewBuilder sideContent: @escaping () -> SideContent) {
self.title = title
self.sideContent = sideContent
}
func body(content: Content) -> some View {
HStack {
VStack {
Text(title)
content
}
sideContent?()
}
}
}
这可以用作SomeView().modifier(SideContent(title: "asdasd") { Text("asdasd")})
,但是,如果省略侧面,则仍需要指定其类型SomeView().modifier(SideContent<EmptyView>(title: "asdasd"))
删除标题所简化的标题,
struct SideContent<SideContent: View>: ViewModifier {
var sideContent: (() -> SideContent)
init(@ViewBuilder sideContent: @escaping () -> SideContent) {
self.sideContent = sideContent
}
func body(content: Content) -> some View {
HStack {
content
sideContent()
}
}
}
此外,您可以为Title
做一个修饰符。
struct Titled: ViewModifier {
var title: String
func body(content: Content) -> some View {
VStack {
Text(title)
content
}
}
}
SomeView()
.modifier(Titled(title: "Title"))
.modifier(SideContent { Text("Side") })