Reputation: 87
I am working on a swift project.In that i need the logo to be shown in left side of navigation bar and would like to make it globally in AppDelegate. But self.navigationitem is not detected in Appdelegate?Any Help would be appreciated as its my first project in swift.
UINavigationBar.appearance().barTintColor = Constants.templatecolor
let logoImage = UIImage.init(named: "logoImage")
let logoImageView = UIImageView.init(image: logoImage)
logoImageView.frame = CGRect(x: -40, y: 0, width: 150, height: 25)
logoImageView.contentMode = .scaleAspectFit
let imageItem = UIBarButtonItem.init(customView: logoImageView)
let negativeSpacer = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
negativeSpacer.width = -25
UINavigationItem.setLeftBarButtonItems(negativeSpacer, imageItem)
Upvotes: 0
Views: 884
Reputation: 20379
You can't set the Navigation item in AppDelegate
. Navigation item is the property of UIViewController
or UINavigationController
and you set the UINavigationItem
for each ViewControllers.
If all that you wanna achieve is setting the navigation title for all screens declare a base class call it as BaseViewController
n in viewDidLoad
of BaseViewController
set the navigation item
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let logoImage = UIImage.init(named: "close")
let logoImageView = UIImageView.init(image: logoImage)
logoImageView.frame = CGRect(x: -40, y: 0, width: 150, height: 25)
logoImageView.contentMode = .scaleAspectFit
let imageItem = UIBarButtonItem.init(customView: logoImageView)
let negativeSpacer = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
negativeSpacer.width = -25
self.navigationItem.leftBarButtonItems = [negativeSpacer,imageItem]
// Do any additional setup after loading the view.
}
}
Now every single viewController in your project can extend from BaseViewController
class ViewController: BaseViewController {
...
override func viewDidLoad() {
super.viewDidLoad()
}
}
Make sure you call super.viewDidLoad
in viewDidLoad
Hope it helps
Upvotes: 2
Reputation: 327
Please change x position of logoImageView while setting its frame. You are sets it's value as -40 might be because of that you are not able to see that logoImage. Change it's x position to 0.
Upvotes: 0