导航栏UINavigationController

1,导航栏上面添加按钮

1.1,根据文字添加
 let rightBarBtn = UIBarButtonItem.init(title: "right", style: 
 UIBarButtonItemStyle.plain, target: self, action: #selector(action_riht))
self.navigationItem.rightBarButtonItem = rightBarBtn

//1.2,系统样式添加     
let leftBarBtn = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.cancel, target: self, action: #selector(action_left))      
 //1.3,图片方式添加

let image = UIImage(named:"icon_iPad-29")?.withRenderingMode(.alwaysOriginal)
let leftBarBtn2 = UIBarButtonItem.init(image: image, style: UIBarButtonItemStyle.done, target: self, action: #selector(action_left))
self.navigationItem.leftBarButtonItem = leftBarBtn2
//        self.navigationItem.leftBarButtonItems = [leftBarBtn,leftBarBtn2]

2,设置导航栏的背景颜色

self.navigationController?.navigationBar.barTintColor = UIColor.brown

3,设置导航栏按钮字体颜色

 self.navigationController?.navigationBar.tintColor = UIColor.cyan

4,设置标题样式与颜色(通过导航栏字典的方式)

self.navigationController?.navigationBar.titleTextAttributes = 
[NSAttributedStringKey.foregroundColor:
UIColor.yellow,NSAttributedStringKey.font:UIFont.systemFont(ofSize: 20)]

5,设置返回按钮的图片,不影响侧滑返回

//        let image = UIImage(named:"icon_iPad-29")?.withRenderingMode(.alwaysOriginal)
//        let navBar = self.navigationController?.navigationBar
//        navBar?.backIndicatorImage = image;
//        navBar?.backIndicatorTransitionMaskImage = image;

6,设置导航栏的背景图片

        self.navigationController?.navigationBar
        .setBackgroundImage(UIImage.init(named: "icon_iPad-29"), for: UIBarMetrics.default)

7,清除导航栏下面的线条

        self.navigationController?.navigationBar.shadowImage = UIImage.init()
        let isTest = false
        if isTest {
8,隐藏导航栏
            self.navigationController?.navigationBar.isHidden = true
9,导航跳转
            //9.1 压栈
            let secondVC = SecondViewController.init()
            self.navigationController?.pushViewController(secondVC, animated: true)
            //9.2 出栈
            //出栈到上级界面
            self.navigationController?.popViewController(animated: true)
            //出栈到顶端
            self.navigationController?.popToRootViewController(animated: true);
            
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容