AppDelegate.swift代码如下:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame:UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.white
self.window?.makeKeyAndVisible()
//1.创建导航控制器的根视图
let vc = ViewController()
//2.创建导航视图控制器,并为他制定根视图控制器
let navigation = UINavigationController(rootViewController: vc)
//3.将导航视图控制器设置为window的根视图控制器
self.window?.rootViewController = navigation
return true
}
ViewController.swift代码如下:
import UIKit
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden = false
}
override func viewDidLoad() {
super.viewDidLoad()
self.setupNavigationBar()
}
func setupNavigationBar() {
//UINavigationController 配置他的时候设置只是当前视图控制器上导航条外观
// self.title = "拨号"
self.navigationItem.title = "消息"
//设置title颜色字体
let dic = [NSFontAttributeName:UIFont.systemFont(ofSize: 30.0),NSForegroundColorAttributeName:UIColor.red]
self.navigationController?.navigationBar.titleTextAttributes = dic
//创建按钮对象
let rightButton = UIBarButtonItem(title: "跳转", style: .plain, target: self, action: #selector(rightButtonAction))
//给右侧rightBarButtonItem赋值
self.navigationItem.rightBarButtonItem = rightButton
//配置左侧的leftBarButtonItem
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(leftButtonAction))
//UINavigationBar 配置他的时候所有视图都会显示,公共的属性,此时是通过导航视图控制器来设置
self.navigationController?.isNavigationBarHidden = false
//bar颜色
// self.navigationController?.navigationBar.barStyle = .black
//bar背景颜色
self.navigationController?.navigationBar.backgroundColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)
//导航条yans
self.navigationController?.navigationBar.barTintColor = UIColor.yellow
//设置控件颜色
self.navigationController?.navigationBar.tintColor = UIColor.black
}
//设置leftButtonAction的关联方法
func leftButtonAction() {
print("左侧按钮")
}
//设置rightButtonAction的关联方法
func rightButtonAction() {
let redVC = RedViewController()
self.navigationController?.pushViewController(redVC, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
RedViewController.swift代码如下:
import UIKit
class RedViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
self.setupNavigationBar()
// self.navigationController?.isNavigationBarHidden = true
//导航条的毛玻璃效果,半透明效果
//导航栏半透明效果(IOS7以后 默认YES)
//当半透明效果开启时 屏幕左上角为坐标原点
//关闭时,导航栏左下角为坐标原点
self.navigationController?.navigationBar.isTranslucent = false
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 150, height: 100)
button.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
button.setTitle("返回", for: .normal)
self.view.addSubview(button)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}
//button关联事件
func buttonAction(){
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.popViewController(animated: true)
}
func setupNavigationBar() {
let left1 = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(leftAction))
let left2 = UIBarButtonItem(barButtonSystemItem: .camera,target:self, action:#selector(left2Action))
self.navigationItem.leftBarButtonItems = [left1,left2]
let titleView = UISegmentedControl(items: ["消息","电话"])
titleView.frame = CGRect(x:0,y:0,width:100,height:30)
titleView.selectedSegmentIndex = 0
//给navigationItem和titleView属性赋值
self.navigationItem.titleView = titleView
}
//left的关联方法
func leftAction(sender:UIBarButtonItem) {
self.navigationController?.popViewController(animated: true)
}
//left2的关联方法
func left2Action(sender:UIBarButtonItem) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}