I trying to make keyboard extension on SwiftUI
. My keyboard uses gestures a lot. But gestures up from keys located near the screen bottom are to be recognized as a system gesture. As it was said here I tried to use preferredScreenEdgesDeferringSystemGestures
parameter of UIInputViewController
but it does not help.
here is my code:
import UIKit import SwiftUI //import CoreData
struct SwiftUIContainer: View{
@State var dragLocation: CGPoint = .zero
var body: some View{
VStack{
Circle()
.fill(Color.red)
.frame(width: 20, height: 20)
.position(self.dragLocation)
.gesture(DragGesture().onChanged(){value in
self.dragLocation = value.location
}
)
}.background(Color.green)
}
}
class MyController<Content: View>: UIHostingController<Content>{
open override var preferredScreenEdgesDeferringSystemGestures: UIRectEdge{
print("request edges for conteiner")
return [.bottom]
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("set edges for conteiner")
setNeedsUpdateOfScreenEdgesDeferringSystemGestures() // better to call it here
}
}
class KeyboardViewController: UIInputViewController {
@IBOutlet var nextKeyboardButton: UIButton!
var swiftUIConteiner: UIViewController!
override var preferredScreenEdgesDeferringSystemGestures: UIRectEdge{
print("request edges for keyboard")
return [.bottom]
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
let swiftUIView = SwiftUIContainer()
let child = MyController(rootView: swiftUIView)
child.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.swiftUIConteiner = child as UIViewController
// child.setNeedsUpdateOfScreenEdgesDeferringSystemGestures()
print("create children for keyboard")
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func viewDidLoad(){
super.viewDidLoad()
view.addSubview(self.swiftUIConteiner.view)
addChild(self.swiftUIConteiner)
print("insert children for keyboard")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("set edges for keyboard")
setNeedsUpdateOfScreenEdgesDeferringSystemGestures() // better to call it here
}
override var childForScreenEdgesDeferringSystemGestures: UIViewController? {
print("show children for keyboard")
return self.swiftUIConteiner
}
}
I tried both, override UIHostingController
as a subclass, and override UIInputViewController
. nothin helps. Is it possible for keyboard to disable system bottom screen edge gesture?
Here how it works now:
and here is how this works in fullscreen app and I want the same with my keyboard: