How to set an image as title in all navigation bars in swift? - swift

I need to know if it is possible to add an image as title on all navigation controllers, this mean, that every controller that had a navigation bar had the same image title.
I used the next code to set an image as title in the navigation bar, but I have to set it on every viewController, the idea its for example set it on AppDelegate as UINavigationBar.appereance() or similar.
let imageView = UIImageView(image: UIImage(named: "my_image.png")!)
imageView.contentMode = .scaleAspectFit
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 107, height: 30))
imageView.frame = titleView.bounds
titleView.addSubview(imageView)
self.navigationItem.titleView = titleView
So the idea is to set this image as the default title for every controller, that way I will just need to configure it one time.
It is that posible? If not, What possible solution do you think that I can apply ?

The title view is a property of each view controller, not the navigation bar or navigation controller.
So start with a UIViewController subclass whose navigation item has this title view, and make all your other view controllers be subclasses of that.

Please try below code,
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
if this not works please share the screenshot.

Related

place NavigationBar subview behind the NavigationBarItem and title

I want to add a view below everything that's on the navigationBar. I need to add them as subviews to my NavigationBar so that they appear on any detail ViewController.
Here is what I tried so far inside my custom NavigationBar subclass (I call this method inside layoutSubviews()):
private var backgroundView = UIView()
backgroundView.frame = CGRect(x: 0, y: 0, width: 100, height: 70)
backgroundView.backgroundColor = .blue
backgroundView.alpha = 0.7
self.insertSubview(backgroundView, at: 0)
And this is what I get:
The backgroundView appears on top of the title and the NavigationBarItem.
What can I do?
According to this post on the apple developer forum, you can do this inside the NavigationBar class (which works!):
self.subviews[0].insertSubview(backgroundView, at: 0)

Pull to refresh indicator gets miss placed when adding imageview behind nav bar

I can't figure out why this happens, but when I add a imageview behind my tableview and navigation bar, my navigation bar acts strange:
The navigation bar gets stuck in a static position and dosenĀ“t move while scrolling as it did before
The activity indicator from pull to refresh gets placed between the navigation bar button and the top of the tableview. Instead of staying on top of the navigation bars title as before.
Left picture is the bug without the imageview in the background.
Right picture show how it works perfectly without the imageview.
My code to get the clear navigation bar (Though I don't think this introduces any problem as it works fine when the imageview in the background isn't there):
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true self.navigationController?.view.backgroundColor = UIColor.clear
Found a solution.
Set the image as the backgroundview of the tableview and set the tableview to be fullscreen.
let image = UIImage(named: "myphoto")
let iv = UIImageView(image: image)
iv.contentMode = .scaleAspectFill
iv.layer.frame = CGRect(x: 0, y: 0, width: (self.tableView.superview?.frame.size.width)!, height: (self.tableView.superview?.frame.size.height)!)
let tableViewBackgroundView = UIView()
tableViewBackgroundView.addSubview(iv)
self.tableView.backgroundView = tableViewBackgroundView

Make UIImage full screen overlaying Nav & Tabbar too

I have a picture that becomes full screen with black bars to preserve ratio. The problem I'm running into is that it doesn't go over the navbar and tabbar as well. I'm guessing the part of the code I need to change is in here
let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
What can I set the newImageView frame to so it covers everything?
The problem is that the full screen image is behind the navigation bar and tab bar.
To set it to the full screen size, use UIScreen.main.bounds:
newImageView.frame = UIScreen.main.bounds
When presenting the full screen image, hide the navigation bar and tab bar:
self.navigationController?.isNavigationBarHidden = true
self.tabBarController?.tabBar.isHidden = true
When dismissing the full screen image, restore the navigation bar and tab bar:
self.navigationController?.isNavigationBarHidden = false
self.tabBarController?.tabBar.isHidden = false
Alternate solution
As #LeoDabus mentioned in the comments, you can solve this by presenting a new viewController which contains the imageView that covers the entire view. Present this viewController modally with:
self.present(newViewController, animated: false, completion: nil)
Note: Setting animated to false will allow the full screen image to appear without animation.

setting titleView of custom UINavigationBar

I have a custom navigation bar that doesn't really do any navigation controlling (app only has one view) its instead used as a header to display the logo against a solid background colour. So I put a navigation bar onto the main view controller and created an outlet so I can reference it in the code. (Using swift by the way).
Right now I'm struggling with setting the logo image in the center of the nav bar. I need to do this with the titleView (instead of setting an entire background image). So far what I've read says to use self.navigationItem.titleView in the view controller, but I think that only works when using a UINavigationController. Here is my code so far:
#IBOutlet weak var navBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
let logoImage = UIImageView(frame: CGRect(x:0, y:0, width: 200, height: 45))
logoImage.contentMode = .ScaleAspectFit
let logo = UIImage(named: "logo.png")
logoImage.image = logo
self.navigationItem.titleView = logoImage
}
But this doesn't work. How do I get access to the titleView using a custom UINavigationBar like this?
You need to access UINavigationBar's topItem attribute.
Then you can set the topItem's titleView with a UIImageView.
So in your case:
self.navBar.topItem?.titleView = logoImage

UITabBarItem Image doesn't show up

I'm using storyboard and a TabBarNavigation Controller. The View Controller of the tabs are embedded in NavigationController. I didn't set the image of the first TabBarItem in the storyboard.
The png files don't show up. Only a gray square is shown, so maybe the size needs to be changed.
If I select the second tab the png file in the first tab will be shown in the correct size.
I couldn't figure that out with the documentation. How do I have to adjust the image or what do I have to do?
var tabBarItem1 : UITabBarItem
var image1 = UIImage(named: "feed.png")
var image2 = UIImage(named: "feed_chosen.png")
tabBarItem1 = tabBar.items[0] as UITabBarItem
tabBarItem1.title = "Feed"
image1.drawInRect(CGRect(x: 0, y: 0, width: 40, height: 30))
tabBarItem1.image = image1
tabBarItem1.selectedImage = image2
Here is a link to some info on how to customize UITabBarController tab images in iOS 7+:
http://appplusme.tumblr.com/post/68723979500/adding-a-custom-selected-uitabbaritem-image-in-ios7
And, of course, there's always Apple's docs:
https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html