Ios 如何嵌入UIViewController';将视图转换为另一个视图,而不中断响应程序链

Ios 如何嵌入UIViewController';将视图转换为另一个视图,而不中断响应程序链,ios,iphone,uiviewcontroller,xcode11,swift4.2,Ios,Iphone,Uiviewcontroller,Xcode11,Swift4.2,我正在尝试为视图控制器实现一个基类,它将添加一个基于特定状态显示和隐藏的顶部横幅。因为我的UIViewController扩展了这个基类,视图属性在我必须将原始视图属性嵌入以编程方式创建的新视图属性时已经加载 嵌入效果很好(UI方面),视图可以适当地自我调整,但是我看到的问题是,嵌入后,嵌入的视图不再响应触摸事件,在我最初嵌入的控制器中,我有一个16行的表视图和一个按钮,在嵌入它之前,它们都正确地响应点击和滚动事件 我受限于这样一个事实,即我无法在IB中使用分割视图控制器来实现双视图分割 这是我

我正在尝试为视图控制器实现一个基类,它将添加一个基于特定状态显示和隐藏的顶部横幅。因为我的
UIViewController
扩展了这个基类,
视图
属性在我必须将原始
视图
属性嵌入以编程方式创建的新
视图
属性时已经加载

嵌入效果很好(UI方面),视图可以适当地自我调整,但是我看到的问题是,嵌入后,嵌入的视图不再响应触摸事件,在我最初嵌入的控制器中,我有一个16行的表视图和一个按钮,在嵌入它之前,它们都正确地响应点击和滚动事件

我受限于这样一个事实,即我无法在IB中使用分割视图控制器来实现双视图分割

这是我当前的实现,似乎无法找出我缺少什么来传播事件,我尝试使用一个自定义视图来覆盖
hitTest()
,但对
newRootView
contentView
变量都无效

非常感谢您的帮助或见解,谢谢

类BaseViewController:UIViewController{
变量isInOfflineMode:Bool{
didSet{if isInOfflineMode{EmbeddeControllerView()}
}
var offlineBanner:UIView!
var contentView:UIView!
私有函数EmbeddeControllerView(){
guard let currentRoot=viewIfLoaded else{return}
currentRoot.TranslatesAutoResizezingMaskinToConstraints=false
让newRootView=UIView(框架:UIScreen.main.bounds)
newRootView.backgroundColor=.yellow//调试
newRootView.isUserInteractionEnabled=true
newRootView.clipsToBounds=true
视图=newRootView
offlineBanner=createOfflineBanner()//返回一个我已验证为可点击的按钮。
view.addSubview(脱机横幅)
contentView=UIView(帧:.0)
contentView.backgroundColor=.cyan//调试
contentView.translatesAutoResizezingMaskintoConstraints=false
contentView.isUserInteractionEnabled=true
view.addSubview(contentView)
contentView.addSubview(currentRoot)
让横幅灯光:CGFloat=40.00
变量topAnchor:NSLayoutYAxisAnchor
变量bottomAnchor:nslayoutyaxanchor
var trailingAnchor:NSLayoutXAxisAnchor
var引线锚点:NSLayoutXAxisAnchor
如果可用(iOS 11.0,*){
topAnchor=view.safeAreaLayoutGuide.topAnchor
bottomAnchor=view.safeAreaLayoutGuide.bottomAnchor
leadingAnchor=视图。安全区域布局指南。leadingAnchor
trailingAnchor=view.safeAreaLayoutGuide.trailingAnchor
}否则{
topAnchor=view.topAnchor
bottomAnchor=view.bottomAnchor
leadingAnchor=视图。leadingAnchor
trailingAnchor=view.trailingAnchor
}
NSLayoutConstraint.activate([
offlineBanner.heightAnchor.constraint(相等常量:bannerHeight),
offlineBanner.topAnchor.constraint(等同于:topAnchor),
offlineBanner.leadingAnchor.constraint(等式:leadingAnchor,常数:0),
offlineBanner.trailingAnchor.constraint(等式:trailingAnchor,常数:0),
])
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(等式:topAnchor,常量:bannerHeight),
contentView.bottomAnchor.constraint(等式:bottomAnchor,常量:0),
contentView.leadingAnchor.constraint(等式:leadingAnchor,常数:0),
contentView.trailingAnchor.constraint(等式:trailingAnchor,常量:0),
])
OfflineViewController.MigrateViewContracts(从:currentRoot到:contentView)
view.setNeedsUpdateConstraints()
offlineBanner.setNeedsUpdateConstraints()
currentRoot.setNeedsUpdateConstraints()
}
私有函数unembedControllerView(){
设v=contentView.subview[0]
v、 removeFromSuperview()
视图=v
OfflineViewController.MigrateViewContracts(从:contentView到:v)
}
/**
替换与当前根目录的安全区域“UILayoutGuide”或实际
当前根视图。
*/
专用静态func MigrateViewContracts(从currentRoot:UIView到newRoot:UIView){
对于currentRoot.constraints中的ct{
变量firstItem:任何?=ct.firstItem
var secondItem:任何?=ct.secondItem
如果可用(iOS 11.0,*){
如果第一项为?UILayoutGuide==currentRoot.SafeArealLayoutGuide{
debugPrint(“迁移第一项是currentLayoutGuide”)
firstItem=newRoot.safeAreaLayoutGuide
}
如果第二项为?UILayoutGuide==currentRoot.SafeArealLayoutGuide{
debugPrint(“迁移第二项是currentLayoutGuide”)
secondItem=newRoot.safeArea布局指南
}
}
如果第一项为?UIView==currentRoot{
debugPrint(“迁移第一项为currentRoot”)
firstItem=newRoot
}
如果第二项为?UIView==currentRoot{
debugPrint(“迁移第二项为currentRoot”)
secondItem=newRoot
}
NSLayoutConstraint.deactivate([ct])
NSLayoutConstraint.activate([
NSLayoutConstraint(项目:第一个项目,如有),
属性:ct.firstAttribute,
关系人:ct.relation,
项目:第二项,
属性:ct.secondAttribute,