I am using Main.StoryBoard with UIViewController, Now i have added SwiftUIView Inside This at top of the Storyboard as
The Code of View Controller is
import UIKit
import SwiftUI
extension UIView {
func addConstrained(subview: UIView) {
subview.translatesAutoresizingMaskIntoConstraints = false
subview.topAnchor.constraint(equalTo: topAnchor).isActive = true
subview.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
subview.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
subview.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
}
class ViewController: UIViewController {
@IBOutlet weak var topNavView: UIView!
let childView = UIHostingController(rootView: TempView())
override func viewDidLoad() {
super.viewDidLoad()
childView.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 152)
addChild(childView)
topNavView.addSubview(childView.view)
childView.didMove(toParent: self)
topNavView.addConstrained(subview: childView.view)
childView.view.isUserInteractionEnabled = true
}
}
The swiftUI View is---
import SwiftUI
struct TempView: View {
var body: some View {
ZStack {
Color.green
VStack {
Button {
print("clicked 1")
} label: {
Text("Button1")
}
.frame(width: 70, height: 60, alignment: .center)
Button {
print("clicked 2")
} label: {
Text("Button2")
}
.frame(width: 70, height: 60, alignment: .center)
}
}
//.background(.red)
.frame(height:100)
}
}
struct TempView_Previews: PreviewProvider {
static var previews: some View {
TempView()
}
}
When i click on Button1 & Button2 I am not able to click. but when i rotate device (Landscape ) it's working fine.. what could be the issue ?
