美文网首页
导航栏,页面跳转

导航栏,页面跳转

作者: 混世小码农 | 来源:发表于2016-11-03 16:26 被阅读0次

导航栏

//ViewController.swift

//UINavigationController

//

//Created by lanou on 16/11/2.

//Copyright (c) 2016年lanou. All rights reserved.

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

//每一个被导航视图控制器所管理的视图控制器都有一个navigationItem(这里包含了左按钮,右按钮,中间标题,中间视图)

//设置导航栏标题

navigationItem.title = "Setting"

let leftBarBtn = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: "leftBtnAction")

//设置导航栏左按钮(UIBarButtonItem)

navigationItem.leftBarButtonItem = leftBarBtn

let rightBarBtn = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "rightBtnAction")

navigationItem.rightBarButtonItem = rightBarBtn

//        navigationItem.leftBarButtonItems = [leftBarBtn,rightBarBtn]

//        navigationItem.rightBarButtonItems = [leftBarBtn,rightBarBtn]

//设置中间视图

let segment = UISegmentedControl(items: ["已接来电","未接来电"])

segment.frame = CGRectMake(0, 0, 100, 30)

segment.selectedSegmentIndex = 0

navigationItem.titleView = segment

//导航栏(UINavigationBar)

//在本类中(视图控制器)访问navigationController就是获取的到本视图控制器所在的导航视图控制器

//设置导航栏是否隐藏

navigationController?.navigationBarHidden = false

//设置导航栏的样式

navigationController?.navigationBar.backgroundColor = UIColor.cyanColor()

//设置导航栏本身的颜色

navigationController?.navigationBar.barTintColor = UIColor.yellowColor()

//设置导航栏元素的颜色

navigationController?.navigationBar.tintColor = UIColor.redColor()

//导航栏半透明效果

navigationController?.navigationBar.translucent = true

let myView = UIView(frame: CGRectMake(0, 0, 60, 60))

myView.backgroundColor = UIColor.blueColor()

view.addSubview(myView)

}

func leftBtnAction(){

print("click left Btn")

}

//跳转到第二个控制器的页面

func rightBtnAction(){

//(1)创建第二个控制器

let secondVC = secondViewController()

//(2)使用当前控制器所在的导航视图控制器跳转到第二个控制器,pushViewController可以进入到下一个页面

navigationController?.pushViewController(secondVC, animated: true)

print("click right Btn")

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

//secondViewController.swift

//UINavigationController

//

//Created by lanou on 16/11/3.

//Copyright (c) 2016年lanou. All rights reserved.

import UIKit

class secondViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

navigationItem.title = "seconrVC"

//设置颜色

view.backgroundColor = UIColor.whiteColor()

let leftBarBtn = UIBarButtonItem(title: "back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction:")

navigationItem.leftBarButtonItem = leftBarBtn

}

func backAction(btn:UIBarButtonItem){

println("返回")

//将secondVC出栈,popViewControllerAnimated:将当前的显示在栈顶的控制器出栈

navigationController?.popViewControllerAnimated(true)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

}

}

//  FifthViewController.swift

//  UINavigationController

//

//  Created by SZT on 2016/11/3.

//  Copyright © 2016年 SZT. All rights reserved.

//

import UIKit

class FifthViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

view.backgroundColor = UIColor.whiteColor()

navigationItem.title = "FifthVC"

let leftBtn = UIBarButtonItem(title: "backToRoot", style: UIBarButtonItemStyle.Plain, target: self, action: "popToRootViewController")

navigationItem.leftBarButtonItem = leftBtn

let btn = UIButton(frame: CGRectMake(100,130,80,45))

btn.setTitle("模态显示", forState: .Normal)

btn.backgroundColor = UIColor.cyanColor()

btn.addTarget(self, action: "presentToSix", forControlEvents: .TouchUpInside)

view.addSubview(btn)

}

//点击按钮模态显示第六个视图控制器

func presentToSix(){

//创建第六个视图控制器

let sixthVC = SixthViewController()

//模态显示,跟导航视图控制器没关系

//参数completion:模态显示完成之后要执行的闭包

presentViewController(sixthVC, animated: true) { () -> Void in

//模态显示动作完成要执行的代码

print("模态动作已完成")

}

}

func popToRootViewController(){

//(1)popToRootViewControllerAnimated:回到根视图控制器

//        navigationController?.popToRootViewControllerAnimated(true)

//        (2)

//先获取到棧里所有的视图控制器

let viewControllers = navigationController?.viewControllers

//获取根视图控制器(因为根视图控制器是最先入棧,所以在第0个下标)

let rootVC:UIViewController = viewControllers![0]

//导航视图控制器返回到指定的视图控制器

navigationController?.popToViewController(rootVC, animated: true)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

//SixthViewController.swift

//UINavigationController

//

//Created by SZT on 2016/11/3.

//Copyright © 2016年SZT. All rights reserved.

import UIKit

class SixthViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

view.backgroundColor = UIColor.grayColor()

let modelBtn = UIButton(frame: CGRectMake(80,150,100,45))

modelBtn.setTitle("模态消失", forState: .Normal)

modelBtn.backgroundColor = UIColor.blueColor()

modelBtn.addTarget(self, action: "dismissViewcontroller", forControlEvents: .TouchUpInside)

view.addSubview(modelBtn)

}

func dismissViewcontroller(){

//    (1)第一种方式:模态过程不可定制化  dismissViewcontroller()

//(2)第二种方式:模态消失过程可定制化(需不需要动画,模态结束后执行代码段)

dismissViewControllerAnimated(true) { () -> Void in

print("模态消失动作已结束")

}

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

/*

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// Get the new view controller using segue.destinationViewController.

// Pass the selected object to the new view controller.

}

*/

}

/*

}

*/

}

/*

页面跳转的方式

1.模态显示

//创建第六个视图控制器

letsixthVC = SixthViewController()

//模态显示,跟导航视图控制器没关系

//参数completion:模态显示完成之后要执行的闭包

presentViewController(sixthVC, animated:true) { () ->Voidin

//模态显示动作完成要执行的代码

print("模态动作已完成")

}

2.push

*/

相关文章

  • SwiftUI页面跳转

    NavigationView导航栏,NavigationLink页面跳转,navigationBarTitle导航...

  • 关于路由跳转导航栏消失的问题

    问题描述: 在做后台管理页面的时候,使用路由跳转的时候,点击导航栏的菜单项,是跳转了,可是页面直接跳转了,导航栏不...

  • 特大新闻

    导航库React Navigation 功能:1.跳转页面 2.底部导航栏 3.顶部导航栏...

  • 参考笔记

    导航库React Navigation 功能:1.跳转页面 2.底部导航栏 3.顶部导航栏...

  • oc navigation跳转

    从显示导航栏页面跳转到隐藏导航栏的页面会闪解决方法 放在loadview里面处理 不要在viewDidLoad处理...

  • 5.26心得

    导航栏: 一级页面跳转时或者二级页面返回一级页面时,会影响双方导航栏的颜色,就是会使导航栏渐变的效果,解决问题就是...

  • 导航栏,页面跳转

    导航栏 //ViewController.swift //UINavigationController // //...

  • navigationBarHidden与navigationBa

    今天做项目,从无导航栏页面跳转包含导航条页面时,使用的是self.navigationController.nav...

  • 小程序自定义导航栏

    1.wxml页面 (自定义导航栏的布局) //css就不粘出来了 2. 自定义导航栏 js (要跳转的页面 对...

  • Flutter:解决首页跳子页面时仍显示底部导航栏问题

    问题描述 Flutter做的app,底部有底部导航栏。点击页面上部跳转到新的页面后,底部导航栏仍然显示。 解决方案...

网友评论

      本文标题:导航栏,页面跳转

      本文链接:https://www.haomeiwen.com/subject/kftiuttx.html