AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
self.window?.makeKeyAndVisible()
//创建导航视图控制器的根视图
let vc = ViewController()
//2.创建导航视图控制器,并为她制定根视图控制器
let navigation = UINavigationController(rootViewController: vc)
//3.将导航视图控制器设置为window的根视图控制器
self.window?.rootViewController = navigation
return true
}
ViewController:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//调用设置导航条的方法
self.setupNavigationBar()
}
func setupNavigationBar() {
// UINavigationItem 配置他的时候设置的只是当前视图控制器上的当行跳的外观
// UINavigationBar 配置他的时候所有的视图都会显示
// 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(rightBarButton))
self.navigationItem.rightBarButtonItem = rightButton
//配置左侧的leftbarbutton
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(leftAction))
// UINavigationBar 配置他的时候所有的视图都会显示:配置的是公共的属性,此时是通过导航视图控制器来控制的
// 显隐性
self.navigationController?.isNavigationBarHidden = false
//导航栏样式
// self.navigationController?.navigationBar.barStyle = .black
//背景颜色 浅
self.navigationController?.navigationBar.backgroundColor = UIColor.green
//深 导航条的颜色
self.navigationController?.navigationBar.barTintColor = UIColor.yellow
//导航条设置控件颜色
self.navigationController?.navigationBar.tintColor = UIColor.black
}
//MARK:-设置leftbarbutton的关联方法
func leftAction() {
print("左侧按钮")
}
//MARK:-设置ringtbutton的关联方法
func rightBarButton() {
//完成跳转
let redVC = redViewController()
self.navigationController?.pushViewController(redVC, animated: true)
}
redViewController:
override func viewDidLoad() {
super.viewDidLoad()
//导航条毛玻璃效果,半透明效果
//导航栏半透明效果(IOS7之后总是yes)
//当半透明效果开启时,屏幕左上角为坐标原点
//关闭时导航栏左下角为坐标原点
self.navigationController?.navigationBar.isTranslucent = false
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.red
self.setupNavigationBar()
// self.navigationController?.isNavigationBarHidden = true
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 150, height: 100)
button.setTitle("返回", for: .normal)
button.backgroundColor = UIColor.black
self.view.addSubview(button)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}
//MARK:-left1的关联方法
func left1Action(sender:UIBarButtonItem) {
self.navigationController?.popViewController(animated: true)
}
//MARK:-left2的关联方法
func left2Action(sender:UIBarButtonItem) {
}