ios - rightBarButtonItem 图像未显示

标签 ios swift3 uinavigationcontroller uitabbaritem

谁能帮我理解我的代码有什么问题。我没有得到它,我的代码编译得很好,没有任何错误,但我的图像没有显示出来。我确定我的图像存在于 中 Assets 文件夹,我也给出了我的图像的正确名称。我正在以编程方式进行操作。以下是我的代码-:

*AppDelegate-:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        windowLayout()
        application.statusBarStyle = .lightContent
        return true
    }

    func windowLayout()
    {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()

        let layout = UICollectionViewFlowLayout()
        window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))
        UINavigationBar.appearance().barTintColor = UIColor(colorLiteralRed: 238/255, green: 32/255, blue: 31/255, alpha: 1)
        let statusBarView = UIView()
        statusBarView.backgroundColor = UIColor(colorLiteralRed: 194/255, green: 31/255, blue: 31/255, alpha: 1)
        window?.addSubview(statusBarView)
        window?.translatesAutoresizingMaskIntoConstraints = false
        //window?.addConstraintsWithFormat(format: "H:|[v0]|", views: statusBarView)
        //window?.addConstraintsWithFormat(format: "V:|[v0(20)]", views: statusBarView)
        UINavigationBar.appearance().shadowImage = UIImage()
        //UINavigationBar.appearance().setBackgroundImage(UIImage(),forBarMatrics: .default)
    }

扩展文件-:
import UIKit


protocol navigationBarItems {
    func searchBarButton()
    func handleMoreButton()
}


extension UINavigationController:navigationBarItems
{
    func handleMoreButton() {

    }

    func searchBarButton()
    {
        let searchBarImage = UIImage(named: "search-icon")?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
        let searchImage = UIBarButtonItem(image: searchBarImage, style: UIBarButtonItemStyle.done, target: self, action: #selector(handleSearch))
        navigationItem.rightBarButtonItem = searchImage

    }

    func handleSearch()
    {
        print("123")
    }

}

Controller 类-:
class HomeController: UICollectionViewController,UICollectionViewDelegateFlowLayout {

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpLayout()
        setUpMenuBar()
        fetchJsonData()
        navigationController?.searchBarButton()
    }

我已经通过设置断点来检查我的函数是否被调用进行了测试。它工作正常,但图像未显示,请帮助。

最佳答案

尝试扩展 UIViewController 而不是 UINavigationController

extension UIViewController:navigationBarItems
{
    func handleMoreButton() {

    }

    func searchBarButton()
    {
        let searchBarImage = UIImage(named: "search-icon")?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
        let searchImage = UIBarButtonItem(image: searchBarImage, style: UIBarButtonItemStyle.done, target: self, action: #selector(handleSearch))
        navigationItem.rightBarButtonItem = searchImage

    }

    func handleSearch()
    {
        print("123")
    }

}

现在从`viewDidLoad`调用这个方法
class HomeController: UICollectionViewController,UICollectionViewDelegateFlowLayout {

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpLayout()
        setUpMenuBar()
        fetchJsonData()
        self.searchBarButton()
    }

关于ios - rightBarButtonItem 图像未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43365833/

相关文章:

ios - 从 UITableViewCell iOS 打开相机

ios - swift 3 : UIViewControllerTransitioningDelegate stopped working

objective-c - 在编写 iOS 应用程序时评论标准?

ios - 使用 UIImagePickerController 为行车记录仪应用程序保存视频

ios - 使用图像选择器将照片保存到 2 个不同的 imageview (swift 3)

ios - 导航栏改变高度

ios - 推送时从UISearchController删除搜索栏

iphone - 通过将导航栏设置为通过推送/弹出隐藏来查看大小

ios - 子类 UITextField 在使用具有自定义字体的尺寸类时覆盖字体

ios - 同一个 ViewController 上的 2 个 PickerViews - titleForRow 基金 "will not be executed"