SwiftUI makes a pervasive utilization of the Builder sample described right here, offering loads of customized modifiers to permit non-obligatory styling of your elements. One of many causes for utilizing this sample is that it permits you to write easy constructors.
However how one can configure properties of view with greater than 10 properties. Clearly we are able to use mannequin the place we are able to wrap all properties inside struct/class. However some instances it should add extra overhead at name web site must create mannequin to configure a single property . Resolution to keep away from that is positively builder sample .
Does beneath method trigger any efficiency points w.r.t view refresh or lifetime.
public struct MyButton<TextContent: View, IconContent: View>: View {
non-public var textual content: String?
non-public var accessibilityText: String?
non-public var dimension: MyButtonSize = .common
non-public var iconName: MyIconName?
non-public var iconPosition: MyButtonIconPosition = .main
non-public var inverse: Bool = false
non-public var disabled: Bool = false
non-public var variant: MyButtonVariant = .major
public var physique: some View { ...}
}
public extension MyButton {
/// units the variant of the button
func variant( _ variant: MyButtonVariant)-> MyButton {
var v = self
v.variant = variant
return v
}
/// units the dimensions of the button.
func dimension( _ dimension: MyButtonSize) -> MyButton {
var view = self
view.dimension = dimension
return view
}
/// units the icon and icon place on button.
func icon( _ identify: MyIconName?, place: MyButtonIconPosition = .main) -> MyButton {
var view = self
view.iconName = identify
view.iconPosition = place
return view
}
// to disable button.
func isDisabled(_ disabled: Bool) -> MyButton {
var view = self
view.disabled = disabled
return view
}
}