美文网首页
Swift 自定义UINavigationBar

Swift 自定义UINavigationBar

作者: gleeeli | 来源:发表于2022-05-28 17:55 被阅读0次

一、初始化

//初始化
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
    }

相关文章

网友评论

      本文标题:Swift 自定义UINavigationBar

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