一、初始化
//初始化
var navBar: UINavigationBar = {
let statusH = UIApplication.shared.statusBarFrame.height
let width = UIScreen.main.bounds.size.width
let frame = CGRect(x: 0, y: statusH, width: width, height: 44)
let nB = UINavigationBar(frame: frame)
return nB
}()
// 设置导航栏透明
navBar.setBackgroundImage(UIImage(), for: .default)
navBar.shadowImage = UIImage()
navBar.isTranslucent = true
let backItem2 = UIBarButtonItem(customView: self.createBackBtn())
let titleItem = UINavigationItem()
titleItem.setLeftBarButtonItems([backItem2], animated: true)
titleItem.titleView = createNavTitleView() // 自定义titleView
navBar.setItems([titleItem], animated: true)
navBar.delegate = self // 设置代理 否则顶部不会充满状态栏
self.view.addSubview(navBar)
实现代理:UINavigationBarDelegate
//充满状态栏
func position(for bar: UIBarPositioning) -> UIBarPosition {
return .topAttached
}











网友评论