Reputation: 2882
Can anyone help me out understand what is wrong in my code . I am not getting it my code compiles fine without any errors but my image is not showing up.I am sure my image is present in Assests folder and I am giving the correct name of my images as well. I am doing it programatically. Below is my code-:
*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)
}
Extension File-:
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-:
class HomeController: UICollectionViewController,UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
setUpLayout()
setUpMenuBar()
fetchJsonData()
navigationController?.searchBarButton()
}
I have tested by putting up a breakpoint to check my function is getting called or not . And it is working fine but image not showing up please help.
Upvotes: 1
Views: 516
Reputation: 72410
Try to make extension of UIViewController instead of 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")
}
}
Now call this method from `viewDidLoad``
class HomeController: UICollectionViewController,UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
setUpLayout()
setUpMenuBar()
fetchJsonData()
self.searchBarButton()
}
Upvotes: 2