//
// WYBaseViewController.swift
// live
//
// Created by 王武 on 2020/12/7.
//
import UIKit
class WYBaseViewController: UIViewController {
// 状态栏背景色
lazy var statusView: UIView = {
let view = UIView()
view.backgroundColor = kMainOrangeColor
view.frame = CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH)
// 设置背景渐变
let gradientLayer: CAGradientLayer = CAGradientLayer()
gradientLayer.colors = kGradientColors
//(这里的起始和终止位置就是按照坐标系,四个角分别是左上(0,0),左下(0,1),右上(1,0),右下(1,1))
//渲染的起始位置
gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
//渲染的终止位置
gradientLayer.endPoint = CGPoint.init(x: 1, y: 0)
//设置frame和插入view的layer
gradientLayer.frame = view.frame
view.layer.insertSublayer(gradientLayer, at: 0)
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = kWhite
// iOS11 中automaticallyAdjustsScrollViewInsets方法被废弃
// 使用UIScrollView的 contentInsetAdjustmentBehavior 属性来替代
automaticallyAdjustsScrollViewInsets = false
}
}
// MARK:- NavigationBar 配置
extension WYBaseViewController {
// 导航栏设置
func setUpNavigation() {
// 修改状态栏背景颜色
self.navigationController?.navigationBar.barTintColor = kMainOrangeColor
self.navigationController?.navigationBar.tintColor = UIColor.white
// 左边的按钮
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "btn_user_normal"), style:.done ,target: self, action: #selector(self.leftItemClick))
// 右边的按钮
navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "viewHistoryIcon"), style:.done, target: self, action: #selector(self.rightItemClick))
// 搜索框
let searchView = WYHomeSearchView()
searchView.layer.cornerRadius = 5
searchView.backgroundColor = kSearchBGColor
navigationItem.titleView = searchView
searchView.snp.makeConstraints { (make) in
make.center.equalTo((navigationItem.titleView?.snp.center)!)
make.width.equalTo(AdaptW(230))
make.height.equalTo(33)
}
}
/// 默认导航栏左边按钮
@objc func leftItemClick() {
self.navigationController?.pushViewController(WYProfileViewController(), animated: true)
}
/// 默认导航栏右边按钮
@objc func rightItemClick() {
// WYPopupView 自定义视图
let customView = WYCustomView.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let popView = WYPopupView(size: CGSize(width: 250, height: 200), customView: customView , style: .WYPopTransition)
popView.wy_showPopView()
}
}
来源:oschina
链接:https://my.oschina.net/wwyywg/blog/4785068