返回到先前的视图控制器的帮助下滑(向左)

我有2个视图控制器。 1: login页面(主视图控制器) 2: 注册页面 。假设我想从注册页面返回到login页面。 如何解决这个问题(与导航控制器),我是新的迅速和iOS。 这是我在AppDelegate.swift中的代码。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window?.backgroundColor = UIColor.whiteColor() self.window?.rootViewController = ViewController() self.window?.makeKeyAndVisible() return true } 

以编程方式embedded导航控制器

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. UINavigationBar.appearance().backgroundColor = UIColor.whiteColor() let loginViewController: LoginViewController = LoginViewController(nibName: "LoginViewController", bundle: nil) let navController: UINavigationController = UINavigationController(rootViewController: loginViewController) window!.makeKeyAndVisible() window!.addSubview(navController.view!) return true } 

embedded式导航控制器使用Storyborad

要启用导航控制器,您需要在导航控制器中embedded您的loginViewController

打开Storyboard – >selectloginviewcontroller – >编辑器(在Xcode菜单中) – >embedded – >导航控制器

在这里输入图像说明

你可以看到结果看起来像

在这里输入图像说明

那么只需更新应用程序委托方法来更改导航栏的背景色

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. UINavigationBar.appearance().backgroundColor = UIColor.whiteColor() return true } 

LoginViewController控制器中添加右滑动手势在viewDidLoad

 override func viewDidLoad() { super.viewDidLoad() let swiperight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(LoginViewController.swiperight(_:))) swiperight.direction = .Right self.view!.addGestureRecognizer(swiperight) } func swiperight(gestureRecognizer: UISwipeGestureRecognizer) { //Do what you want here //Load Signup view controller here func swiperight(gestureRecognizer: UISwipeGestureRecognizer) { //Load Signup view controller here let signupHomeViewController: SignupHomeViewController = SignupHomeViewController(nibName: nil, bundle: nil) // and push it onto the 'navigation stack' self.navigationController?.pushViewController(signupHomeViewController, animated: true) } } 

SignupViewController控制器中添加viewDidLoad中的左滑动手势

 override func viewDidLoad() { super.viewDidLoad() let swipeleft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(SignupViewController.swipeleft(_:))) swipeleft.direction = .Left self.view!.addGestureRecognizer(swipeleft) } func swipeleft(gestureRecognizer: UISwipeGestureRecognizer) { //Do what you want here //Pop back to login view controller } 

根据你的问题,我认为你已经创build了login页面,注册页面,并能够导航到注册页面。 由于您使用的是导航控制器,所以您必须使用push to viewcontroller。 你可以在左边添加一个barbutton,按如下定义一个action

 var b = UIBarButtonItem( title: "Back To Login", style: .Plain, target: self, action: "backToLogin:" ) func backToLogin(sender: UIBarButtonItem) { self.navigationController?.popViewControllerAnimated(true) } 

对于滑动function:

 var leftGesture = UISwipeGestureRecognizer(target: self, action: Selector("swipeToLogin:")) leftGesture.direction = .Left self.view.addGestureRecognizer(leftGesture) func swipeToLogin(sender:UISwipeGestureRecognizer) { self.navigationController?.popViewControllerAnimated(true) }