IOS实现右滑返回上一界面

先上效果图:

IOS实现右滑返回上一界面_第1张图片

实现步骤:

1、创建一个single view app

2、清空Main Interface输入框

3、项目结构

IOS实现右滑返回上一界面_第2张图片

4、代码部分

AppDelegate.swift代码

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.

        self.window = UIWindow(frame: UIScreen.main.bounds)

        let homeViewController = HomeViewController(nibName:"HomeViewController",bundle:nil)

        let navgationController = UINavigationController(rootViewController: homeViewController)

        self.window?.rootViewController = navgationController

        self.window?.makeKeyAndVisible()

        return true

    }

HomeViewController.swift代码(继承UIViewController)

  @IBAction func login(_ sender: Any) {

        let loginViewController = LoginViewController(nibName:"LoginViewController",bundle:nil)

        self.navigationController?.pushViewController(loginViewController, animated: true)

        //self.present(loginViewController, animated: true, completion: nil)

    }

    @IBAction func register(_ sender: Any) {

        let registerViewController = RegisterViewController(nibName:"RegisterViewController",bundle:nil)

        //self.present(registerViewController, animated: true, completion: nil)

        self.navigationController?.pushViewController(registerViewController, animated: true)

    }
    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        self.setupNav()

    }

    func setupNav() {

        //隐藏导航栏

        self.navigationController?.isNavigationBarHidden = true

    }
RegisterViewController.swift、LoginViewController.swift继承BaseUIViewController.swift

RegisterViewController.swift代码   

class RegisterViewController: BaseUIViewController {
    @IBAction func back(_ sender: Any) {

        //self.dismiss(animated: true, completion: nil)

        self.navigationController?.popViewController(animated: true)

    }

LoginViewController.swift代码

class LoginViewController: BaseUIViewController {
    @IBAction func back(_ sender: Any) {

        //self.dismiss(animated: true, completion: nil)

        self.navigationController?.popViewController(animated: true)

    }

BaseUIViewController.swift代码(主要功能:开启右滑手势

class BaseUIViewController: UIViewController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

        openSwipe()

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    func openSwipe(){

        if(self.navigationController != nil){

            self.navigationController!.interactivePopGestureRecognizer!.delegate = self;

        }

    }

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {

        if self.navigationController?.viewControllers.count == 1{

            return false;

        }

        return true;

    }
}

5、参考的文章

(1)swift中UINavigationController的使用

(2)Swift之导航控制器(UINavigationController)

(3)Swift NavigationBar隐藏后的右滑手势







你可能感兴趣的:(IOS)