SWRevealViewController:当后视图(菜单)打开时更改前视图的颜色

时间:2017-03-28 20:57:28

标签: ios swift swrevealviewcontroller

我正在使用Swift 3和SWRevealViewController来创建侧栏菜单应用程序。

当后视图(菜单)打开时,如何控制前视图的颜色? 我想显示灰色(如禁用/透明)。 到目前为止,无论我尝试过什么都没有工作,也无法在线找到直接的解决方案。 我可以控制后视色而不是前视图。

到目前为止,我已尝试在后视图控制器的viewWillAppear中更改前视图颜色。

MenuController中的viewWillAppear如下所示:

覆盖func viewWillAppear(_ animated:Bool){

print("MenuController - viewWillAppear")
super.viewWillAppear(animated)
self.revealViewController().frontViewController.view.isUserInteractionEnabled = false
self.revealViewController().view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
self.revealViewController().frontViewController.view.backgroundColor = UIColor.blue
self.revealViewController().rearViewController.view.backgroundColor = UIColor.gray
}

它会改变后视图的颜色,但不适用于前视图。 我希望灰色禁用看起来不仅仅是改变背景颜色。

1 个答案:

答案 0 :(得分:0)

尝试将其添加到rearVieController类

中的viewWillAppear方法中
revealViewController.frontViewController.view.backgroundColor = UIColor.blue //Set colour to what ever you want

所以看起来应该是这样的

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    revealViewController.frontViewController.view.backgroundColor = UIColor.blue
}

编辑:

好的,我试过这样做,似乎有效

如果您创建一个新的UIView并将其添加到frontViewController,则在视图消失时将其删除。

var view: UIView?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    //Create the view the same size as your frontViewController
    view = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(revealViewController.view.frame.size.width), height: CGFloat(revealViewController.view.frame.size.height)))
    //Set the colour of the view to whatever you like
    view?.backgroundColor = UIColor.blue
    //Add the new view to the frontViewcontroller
    revealViewController.frontViewController.view.addSubview(view!)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    //Remove the view from the frontViewController
    view?.removeFromSuperview()
}

编辑:

如果您向UIView添加点击手势,则删除点击功能中的视图而不是viewWillDisappear

var view: UIView?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    //Create the view the same size as your frontViewController
    view = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(revealViewController.view.frame.size.width), height: CGFloat(revealViewController.view.frame.size.height)))
    //Set the colour of the view to whatever you like
    view?.backgroundColor = UIColor.blue
    //Add a tap gesture to the UIview
    let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
    view?.addGestureRecognizer(tap)
    //Add the new view to the frontViewcontroller
    revealViewController.frontViewController.view.addSubview(view!)
}

func handleTap(_ sender: UITapGestureRecognizer) {
    //Remove the view from the frontViewController
    view?.removeFromSuperview()
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    //Moved the remove to the tap gesture function
}