I am trying to pick a clipShape based on a boolean value.
.clipShape(
noText ? Circle() : Capsule()
)
this will not work because of this error:
Result values in '? :' expression have mismatching types 'Circle' and 'Capsule'
What is the best way to accomplish selecting a clip-shape based on a boolean value? Thanks
2
Answers
Your ? : expression need to ensure that both conditions are the same type, so to solve your problem just wrap your shape inside AnyShape(). Try below sample code(you can replace with your own image):
Here’s a possible solution using custom modifier:
To create a custom modifier, first create a struct that conforms to
ViewModifier
. TheViewModifier
protocol requires one thing which is the struct to have this method:func body(content: Content) -> some View
. Inside the method, you can customize your custom modifier.Then you can use your custom modifier like this:
But if you’re planning to use frequently, you can add it to the View Extension and use it like this too: