通过向后滑动(向左)返回到上一个视图控制器

2024-04-09

我有 2 个视图控制器。 1:登录页面(主视图控制器) 2:注册页面.假设我想从Sign Up页至Login页 。如何解决这个问题(使用导航控制器),我是 swift 和 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
}

以编程方式嵌入导航控制器

 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
    }

使用 Storyboard 嵌入导航控制器

要启用导航控制器,您需要将登录 ViewController 嵌入到导航控制器中

打开 Storyboard --> 选择 loginviewcontroller --> 编辑器(在 Xcode 菜单中)--> 嵌入 --> 导航控制器

你可以看到结果看起来像

然后只需更新应用程序委托方法即可更改导航栏背景颜色

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        UINavigationBar.appearance().backgroundColor = UIColor.whiteColor()

        return true
    }

In 登录视图控制器控制器在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)

    }

    }

In 注册视图控制器控制器在 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

    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过向后滑动(向左)返回到上一个视图控制器 的相关文章

随机推荐