When i am using a swiftUI's button with the help of a UIHostingController inside a UIKit based view ,
Swift UI view with button : -
@State var uTitle:String!
@State var uFrame:CGRect!
var body: some View {
Button(action: {
self.ButtonAction(pId: uId)
}) {
Text(uTitle)
}
}
func ButtonAction (pId:Int)
{
print ("Hello world !!!!!")
}
}
If i use above SwiftUIButton this way ( Adding button.frame.size ) :-
let button:UIView!
button = UIHostingController(rootView: SwiftUIButton (uTitle: "Button title")).view
button.frame.origin = CGPoint(x: 50, y: 50)
button.frame.size = CGSize(width: 150, height: 50)
parentview.addSubview(button)
Button click action works fine.
But if i use button this way (not adding width height after intantiation of UIHostingController ,instead adding width height inside SwiftUIButton using .frame(widht: ,height:))
let button:UIView!
button = UIHostingController(rootView: SwiftUIButton (uTitle: "Button title")).view
button.frame.origin = CGPoint(x: 50, y: 50)
// Removed frame.size
parentview.addSubview(button)
@State var uTitle:String!
@State var uFrame:CGRect!
var body: some View {
Button(action: {
self.ButtonAction(pId: uId)
}) {
Text(uTitle)
// Added .frame property
.frame(width:150,height:50)
}
}
func ButtonAction (pId:Int)
{
print ("Hello world !!!!!")
}
}
Action is not triggering. What could be the issue?