我正在申请注册过程主屏幕出现后的应用程序。并且SWRevealViewController已在主屏幕中配置。
我想以编程方式配置SWRevealViewController。
以下是我的代码
let storyboard = UIStoryboard(name: "MainSW", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let rvc:SWRevealViewController = self.revealViewController() as SWRevealViewController
rvc.pushFrontViewController(vc, animated: true)
但它给我的错误如下。
无法转换类型的值' UINavigationController' (0x110d9f860)到 ' SWRevealViewController' (0x10dc346b8)。
我也试过以下链接
SWRevealViewController solution
但它对我没有帮助。
答案 0 :(得分:0)
这是我的配置方式:
我已经创建了2个UIBarButtonItem
作为HomeViewController
的属性(我将在代码中对其进行设置)
let leftButton = UIBarButtonItem()
let rightButton = UIBarButtonItem()
在 viewDidLoad 方法中:
revealViewController().delegate = self
在 viewWillAppear 方法中:
if (revealViewController() != nil) {
revealViewController().rearViewRevealWidth = -50
revealViewController().rightViewRevealWidth = -50
leftButton.image = UIImage(named: "yourImageName")! // check that the image exists, don't use !
leftButton.style = .plain
leftButton.target = revealViewController()
leftButton.action = #selector(SWRevealViewController.revealToggle(_:))
rightButton.image = UIImage(named: "yourImageName")! // check that the image exists, don't use !
rightButton.style = .plain
rightButton.target = revealViewController()
rightButton.action = #selector(SWRevealViewController.rightRevealToggle(_:))
view.addGestureRecognizer(revealViewController().panGestureRecognizer())
view.addGestureRecognizer(revealViewController().tapGestureRecognizer())
}
这是我用来在HomeViewController中设置SWRevealViewController以便具有左右菜单的所有代码。
您现在可以使用委托方法自定义行为:
extension HomeViewController: SWRevealViewControllerDelegate {
func revealController(_ revealController: SWRevealViewController!, willMoveTo position: FrontViewPosition) {
view.alpha = position == FrontViewPosition.left ? 1.0 : 0.6
}
}
我在HomeViewController上设置了一个alpha,以使其不在前面时变暗。
对于情节提要部分:
rear
和right
segue)将SWRevealViewController链接到2个UIViewControllers front segue
将SWRevealViewController链接到您的UINavigationController。root relationship
)请记住,您可能还想为RegisterViewController和HomeViewController之间的过渡创建自定义序列。
您也可以通过编程方式创建它:
class RegisterToHomeSegue: UIStoryboardSegue {
override func perform() {
let source = self.source as! RegisterViewController
let homeVC = self.destination as! HomeViewController
let window = source.view.window
window?.rootViewController = homeVC
window?.addSubview(source.view)
UIView.transition(from: source.view, to: homeVC.view, duration: 0.50, options: .transitionCrossDissolve, completion: nil)
}
}
然后在Storyboard
中将班级选择从您的RegisterViewController
更改为SWRevealViewController
到这个新的RegisterToHomeSegue
并为其指定一个标识符。这样您就可以在RegisterViewController
中致电:
performSegue(withIdentifier: "yourIdentifier", sender: nil)
还有voilà
,您现在有了:
告诉我是否有帮助。