Opaque types don't seem to be made for definitions like this. There's a lot of interesting information about this on this SO thread: What is the `some` keyword in Swift(UI)?
You can type the variable as AnyView
and use AnyView()
to wrap/type erase the content -- then you can use any modifiers you want.
let txt: AnyView = AnyView(Text("Hi").frame(width: 20, height: 20, alignment: .center))
I particularly like this extension to make it look a little cleaner:
extension View {
func eraseToAnyView() -> AnyView {
return AnyView(self)
}
}
let txt: AnyView = Text("Hi").frame(width: 20, height: 20, alignment: .center).eraseToAnyView()