NavigationBar 常用自定义方法总结

class ViewController: UIViewController {

    @IBOutlet weak var customSegmentControl: UISegmentedControl!

    struct SegmentedControl {

        static let textButton = 0

        static let imageButton = 1

        static let controlButton = 2

        static let customTitleView = 3

    }

    override func viewWillAppear(_ animated: Bool) {

        self.title = "二级页面"

        self.navigationController?.setNavigationBarHidden(false, animated: true)

    }

    

    override func viewDidLoad() {

        super.viewDidLoad()

        layoutSubViews()

    }

    

    func layoutSubViews() {

        self.view.backgroundColor = UIColor.white

        //定制化navigationbar

        self.navigationController!.navigationBar.barStyle = .black //系统只有两种选择,default和black

        self.navigationController!.navigationBar.isTranslucent = true //是否透明

        //foregroundColor 文字的前景色就是字体的颜色

        //backgroundColor 文字的背景色

        //strikethroughStyle 删除线,后面参数是设置的删除线的宽度

        //underlineStyle 下划线,后面参数是设置的下划线的宽度

        //stroke 描边

        //shadow 阴影

        //font 字体大小

        let shadow = NSShadow.init()

        shadow.shadowColor = UIColor.systemPink

        shadow.shadowBlurRadius = 8

        shadow.shadowOffset = CGSize.init(width: 10, height: 10)

        //附件attachment

        let attachment = NSTextAttachment.init(image: UIImage.init(named:"Image") ?? UIImage.init())

        self.navigationController!.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white, .backgroundColor:UIColor.red, .strikethroughStyle:2,.underlineStyle:1,.strokeColor:UIColor.blue, .strokeWidth:3,.shadow:shadow,.font:UIFont.systemFont(ofSize: 40)] //定义navigationbar的title text属性

        //#colorLiteral(red: 1, green: 0.99997437, blue: 0.9999912977, alpha: 1)

        self.navigationController!.navigationBar.tintColor =  colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1) //修改navigationbar的返回键的图标和文字颜色

        self.navigationController?.navigationBar.barTintColor =  colorLiteral(red: 0.9797042505, green: 0.5248824416, blue: 0.5065333076, alpha: 1) //修改navigationbar的整体背景颜色

        self.navigationController?.navigationBar.setBackgroundImage(UIImage.init(named: "Image"), for: UIBarMetrics.default)//修改navigationbar的背景图片

        self.navigationController?.navigationBar.setTitleVerticalPositionAdjustment(0, for: UIBarMetrics.default)//提供了title向上或者向下垂直偏移的幅度,正数是向下偏移,负数是向上偏移

        self.navigationController?.navigationBar.setTitleVerticalPositionAdjustment(-5, for: UIBarMetrics.compact)//可以设计横屏样式

        //promp 提示会在navigationbar头部位置展示自定义的文本信息

        navigationItem.prompt = NSLocalizedString("Navigation prompts appear at the top.", comment: "")

        //self.navigationController?.navigationBar.prefersLargeTitles = true

        

        // Make the navigation bar's title with red text.

        //        let appearance = UINavigationBarAppearance()

        //        appearance.configureWithOpaqueBackground()

        //        appearance.backgroundColor = UIColor.systemRed

        //        appearance.titleTextAttributes = [.foregroundColor: UIColor.lightText] // With a red background, make the title more readable.

        //        navigationItem.standardAppearance = appearance

        //        navigationItem.scrollEdgeAppearance = appearance

        //        navigationItem.compactAppearance = appearance // For iPhone small navigation bar in landscape.

        //

        //        // Make all buttons with green text.

        //        let buttonAppearance = UIBarButtonItemAppearance()

        //        buttonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.systemGreen]

        //        navigationItem.standardAppearance?.buttonAppearance = buttonAppearance

        //        navigationItem.compactAppearance?.buttonAppearance = buttonAppearance // For iPhone small navigation bar in landscape.

        //

        //        // Make the done style button with yellow text.

        //        let doneButtonAppearance = UIBarButtonItemAppearance()

        //        doneButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.systemYellow]

        //        navigationItem.standardAppearance?.doneButtonAppearance = doneButtonAppearance

        //        navigationItem.compactAppearance?.doneButtonAppearance = doneButtonAppearance // For iPhone small navigation bar in landscape.

        

    }

    

    @IBAction func action(_ sender: AnyObject) {

        Swift.debugPrint("CustomRightViewControll ibaction invoked.")

    }

    

    //navigationItem 有几个常用的属性,titleview rightbarbuttonitem,leftbarbuttonitem,rightbarbutton, leftbarbutton, title

    @IBAction func customSegmentControlSelected(_ sender: UISegmentedControl) {

        switch sender.selectedSegmentIndex {

        case SegmentedControl.textButton:

            let addButton = UIBarButtonItem(title: NSLocalizedString("AddTitle", comment: ""),

                                            style: .plain,

                                            target: self,

                                            action: #selector(action(_:)))

            navigationItem.rightBarButtonItem = addButton

        case SegmentedControl.imageButton:

            let emailButton = UIBarButtonItem(image: UIImage(systemName: "envelope")!,

                                              style: .plain,

                                              target: self,

                                              action: #selector(action(_:)))

            navigationItem.rightBarButtonItem = emailButton

        case SegmentedControl.controlButton:

            let segmentedControl = UISegmentedControl(items: [

                UIImage(systemName: "arrow.up")!,

                UIImage(systemName: "arrow.down")!

            ])

            

            segmentedControl.addTarget(self, action: #selector(action), for: .valueChanged)

            segmentedControl.frame = CGRect(x: 0, y: 0, width: 90, height: 30)

            segmentedControl.isMomentary = true

            

            let segmentBarItem = UIBarButtonItem(customView: segmentedControl)

            navigationItem.rightBarButtonItem = segmentBarItem

        case SegmentedControl.customTitleView:

            let segmentTextContent = [

                NSLocalizedString("Image", comment: ""),

                NSLocalizedString("Text", comment: ""),

                NSLocalizedString("Video", comment: "")

            ]

            

            let segmentedControl = UISegmentedControl(items: segmentTextContent)

            segmentedControl.selectedSegmentIndex = 0

            segmentedControl.autoresizingMask = .flexibleWidth

            segmentedControl.frame = CGRect(x: 0, y: 0, width: 400, height: 30)

            segmentedControl.addTarget(self, action: #selector(action(_:)), for: .valueChanged)

            self.navigationItem.titleView = segmentedControl

        default:

            break

        }

    }

}

//这里需要注意,如果想修改back按钮的文字,实际上需要在之前一个viewcontroller中设置,这样push出来后就是被改变后的显示内容

        let backBarButtton = UIBarButtonItem(title: "测试", style: .plain, target: nil, action: nil)

        navigationItem.backBarButtonItem = backBarButtton

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值