当我尝试在普通的ViewController中使用InputAccessoryView时,它可以很好地工作。但是当我尝试在Sub ViewController中使用它时,它就不再显示了。
以前有人在这面对吗?
class ViewController: UIViewController {
let myViewController = MyViewController()
override func viewDidLoad() {
super.viewDidLoad()
myViewController.view.frame = CGRect(x: 0.0, y: 200.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 200.0)
self.addChildViewController(self.myViewController)
self.view.addSubview(self.myViewController.view)
myViewController.didMove(toParentViewController: self)
}
}
class MyViewController: UIInputViewController {
override var canBecomeFirstResponder: Bool {
return true
}
override var inputAccessoryView: UIView? {
let view = UIView()
view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50)
view.backgroundColor = UIColor.red
return view
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .green
}
}
答案 0 :(得分:0)
添加控制器作为子视图应该是这样的:
self.addChildViewController(self.myViewController)
self.view.addSubview(self.myViewController.view)
self.myViewController.didMove(toParentViewController: self)