美文网首页
iOS 导航栏的设置

iOS 导航栏的设置

作者: Eyes_cc | 来源:发表于2020-05-13 21:44 被阅读0次
效果图

一、设置backBarButtonItem按钮

self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "BackArrow_Nav")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "BackArrow_Nav")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

二、设置导航栏渐变色

import UIKit
import IQKeyboardManagerSwift

internal class BaseController: UIViewController {

    
    var returnKeyHandler: IQKeyboardReturnKeyHandler!
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        MobClick.beginLogPageView(NSStringFromClass(type(of: self).self))
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        MobClick.endLogPageView(NSStringFromClass(type(of: self).self))
        
        self.returnKeyHandler = nil
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
}

extension BaseController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // 隐藏黑线设置无蒙版效果
        self.navigationController?.hidesNavigationBarHairline = true
        self.navigationController?.navigationBar.isTranslucent = false
        
        //
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
        self.navigationController?.navigationBar.tintColor = UIColor.white
//        self.navigationController?.navigationBar.barTintColor = UIColor(hexString: "#1663be")
        self.view.backgroundColor = UIColor.white
        
        self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "BackArrow_Nav")
        self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "BackArrow_Nav")
        self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        navigationController?.navigationBar.apply(gradient: [UIColor(hexString: "#3496F4"), UIColor(hexString: "#75B4FF")])
        
        
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        self.navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont.adjustFontSize(ofSize: 15)], for: .normal)
        self.navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont.adjustFontSize(ofSize: 15)], for: .normal)
        self.navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont.adjustFontSize(ofSize: 15)], for: .normal)
    }
}

extension UINavigationBar {
    
    /// Applies a background gradient with the given colors
    func apply(gradient colors : [UIColor]) {
        var frameAndStatusBar: CGRect = self.bounds
        frameAndStatusBar.size.height += 20 // add 20 to account for the status bar
        setBackgroundImage(UINavigationBar.gradient(size: frameAndStatusBar.size, colors: colors), for: .default)
    }
    
    /// Creates a gradient image with the given settings
    static func gradient(size : CGSize, colors : [UIColor]) -> UIImage? {
        // Turn the colors into CGColors
        let cgcolors = colors.map { $0.cgColor }
        
        // Begin the graphics context
        UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
        
        // If no context was retrieved, then it failed
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        
        // From now on, the context gets ended if any return happens
        defer { UIGraphicsEndImageContext() }
        
        // Create the Coregraphics gradient
        var locations : [CGFloat] = [0.0, 1.0]
        guard let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: cgcolors as NSArray as CFArray, locations: &locations) else { return nil }
        
        // Draw the gradient
        context.drawLinearGradient(gradient, start: CGPoint(x: 0.0, y: 0.0), end: CGPoint(x: size.width, y: 0.0), options: [])
        
        // Generate the image (the defer takes care of closing the context)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

import Foundation
@objc protocol BackButtonHandlerProtocol {
    @objc optional func navigationShouldPopOnBackButton() -> Bool
}
extension UIViewController: BackButtonHandlerProtocol {}

extension UINavigationController: UINavigationBarDelegate {
    public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
        if viewControllers.count < (navigationBar.items?.count ?? 0) {
            return true
        }
        
        let shouldPop = (topViewController as! BackButtonHandlerProtocol).navigationShouldPopOnBackButton?() ?? true
        if shouldPop {
            DispatchQueue.main.async(execute: {
                self.popViewController(animated: true)
            })
        } else {
            for subView in navigationBar.subviews {
                if subView.alpha < 1 {
                    UIView.animate(withDuration: 0.25, animations: {
                        subView.alpha = 1
                    })
                }
            }
        }
        return false
    }
}

相关文章

网友评论

      本文标题:iOS 导航栏的设置

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