How to add a UIView into navigationItem.backBarButtonItem - ios

I am trying to achieve this
this is my code
let contactName=UILabel()
contactName.text=chat?.Name
contactName.font=UIFont(name: "System", size: 17)
contactName.sizeToFit()
let contactImg=UIImageView()
contactImg.image=UIImage(named: (chat?.Image)!)
contactImg.frame.size.width=20
contactImg.frame.size.height=20
contactImg.layer.cornerRadius=contactImg.frame.height/2
let backButtonView=UIView()
backButtonView.addSubview(contactImg)
backButtonView.addSubview(contactName)
backButtonView.frame.size.width=20+contactName.frame.width
backButtonView.frame.size.height=max(contactImg.frame.height,contactName.frame.height)
navigationItem.backBarButtonItem=UIBarButtonItem(customView: backButtonView)
but when I run app it shows the default backbarbutton
what am I doing wrong here please guide.

let profileImage = UIImage(named: "profile_image")
self.navigationController?.navigationBar.backIndicatorImage = profileImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = profileImage
self.navigationController?.navigationBar.backItem?.title = "Jhon"

Aditya is right, but here's a little more explanation. From the Apple docs:
When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.
https://developer.apple.com/documentation/uikit/uinavigationitem/1624958-backbarbuttonitem
Also, the UIBarButtonItem is "...specialized for placement on a toolbar or tab bar. You typically use Interface Builder to create and configure bar button items."
https://developer.apple.com/documentation/uikit/uibarbuttonitem

Related

How to select tab bar item without selecting it

I am using swift and I have a problem with the tab bar. I want to select a tab bar item, but without open it, what I mean: I want to change its color like it is selected but without the selection is it possible to be done. Here is a image: https://imgur.com/a/IdPituS , I want the top left item (burger menu item) color to be like the first one and the first one color to be light (like not selected).
Try to edit the image of the item you want to highlight (selecting without selecting).
You can try something like this:
let image = shouldHighlight ? UIImage(named: "highlightItem") : UIImage(named: "normalStyle")
myViewController.tabBarItem = UITabBarItem(title: "text", image: image, selectedImage: UIImage(named: "selectedImage"))
Then you update the viewControllers property in you tab bar viewController
myTabViewController.viewControllers
You can pre-select the tabBar item
like this:
tabBarController?.selectedIndex = 0 //select whatever You want
and
colorTint the unselected index
tabBar = UITabBarController()
tabBar?.tabBar.barTintColor = UIColor.white //bar Color
tabBar?.tabBar.tintColor = .red //Icons color
tabBar?.tabBar.unselectedItemTintColor = .white //Unselected item Colors

How to set bar button item background image globally in iOS 13

iOS 13 has a whole new set of classes for configuring navigation bars and bar button items:
UIBarAppearance
UINavigationBarAppearance
UIBarButtonItemAppearance
UIBarButtonItemStateAppearance
How do I use these to give bar button items a global appearance? Let's say I want all bar button items to have a common background image. I used to say:
UIBarButtonItem.appearance().setBackgroundImage(im, for:.normal, barMetrics:.default)
This seems to be just what is superseded by these new classes. So what's the right way now?
It's verbose, but in some ways it's clearer. You can configure a navigation bar appearance and assign it to the navigation bar's standardAppearance through the proxy:
let app = UIBarButtonItemAppearance()
app.normal.backgroundImage = im
let navbarapp = UINavigationBarAppearance()
navbarapp.configureWithOpaqueBackground()
navbarapp.buttonAppearance = app
UINavigationBar.appearance().standardAppearance = navbarapp
The only problem with that is that it assigns the same background image to back button items. So if that's not desired, you have to code defensively, assigning back button items an empty (not nil) image:
let app = UIBarButtonItemAppearance()
app.normal.backgroundImage = im
let navbarapp = UINavigationBarAppearance()
navbarapp.configureWithOpaqueBackground()
navbarapp.buttonAppearance = app
let back = UIBarButtonItemAppearance()
back.normal.backgroundImage = UIImage() // prevent back button item
navbarapp.backButtonAppearance = back
UINavigationBar.appearance().standardAppearance = navbarapp

Display only text without image for UITabBarItem?

I want to display only title for tabbar item without image. Is it possible? When I tried setting
let tabItem = UITabBarItem()
tabItem.title = "Tab1"
tabItem.selectedImage = nil
tabItem.image = nil
tabViewController?.tabBarItem = tabItem
I'm just getting a blank space which is tapable, but no title or image.
simplest way is remove the images from the tab bar and set the custom offset vertical.as shown in the image. or else follow the link that TJ3n priveded remove the images in the UITabBarItem and aligned vertically the title.
In case you want it to be done programmatically:
tabItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -16)

Change image of UITabBarItem

I need to set a different image for the more button in the UITabBar in Swift 2.
I tried this but it doesn't work.
let more = tabBarController.moreNavigationController.tabBarItem
more.image = UIImage(named: "squadra")?.imageWithRenderingMode(.AlwaysOriginal)
Really thanks.
You can do it from interface builder as follows:
Open document outline and select tab bar item as follows:
Then change image property of bar item from attribute inspector to appropriate xcasset as follows:
Finally your image of tab bar item will be changed
tabBarController.tabBar.items?.first?.image = UIImage(named: "squadra")?.imageWithRenderingMode(.AlwaysOriginal)

Subclassing UINavigationBar with Swift

I'm writing an app that gives a user tokens to spend and I want to display the user's current number of tokens in a UINavigationBar. What I would like to have is a label with the number of tokens, and an image of a coin in the top right corner of my navigation bar.
I've been searching for ways to customize the UINavigationBar, and have found plenty of posts related to adding an image to cover the entire bar, and changing the title. However, I can't find a simple way to do what I want.
I think I need to subclass UINavigationBar and add the text/image myself, but being new to iOS development and Swift, I was hoping that someone could point me in the right direction.
You can create a custom view contains subviews UILabel and UIImageView to show the token number and token image. Add it to right bar button item of the navigation controller.
It will look like:
Below code will create the custom view. Here you can observe that it is a local variable. However, you can manage global variable for custom view or create a whole new class and manage it independently for real-time updates to show token number.
// Custom to hold token number and image
let tokenView = UIView(frame: CGRect(x:0, y:0, width:100, height:44))
tokenView.backgroundColor = UIColor.yellow
// Label to show token number
let tokenLabel = UILabel(frame: CGRect(x:0, y:0, width:60, height:44))
tokenLabel.text = "1234"
tokenLabel.textAlignment = NSTextAlignment.right
let imageHeight = CGFloat(30)
let marginY = CGFloat((tokenView.frame.size.height / 2) - (imageHeight / 2))
// ImageView to display token image
let tokenImage = UIImageView(image: UIImage(named: "coin"))
tokenImage.frame = CGRect(x:70, y:marginY, width:30, height:30)
tokenView.addSubview(tokenLabel)
tokenView.addSubview(tokenImage)
// Add custom view as a right bar button item
let barButtonItem = UIBarButtonItem(customView: tokenView)
self.navigationItem.rightBarButtonItem = barButtonItem
To display the current number of tokens you can just set the title. In your view controller:
navigationItem.title = '\(numberOfCoins) coins"
If you want to do anything more fancy you can set your own title view instead of the standard UILabel:
navigationItem.titleView = UIView(...) // your custom view
To show the coin in the top right corner you'd set the rightBarButtonItem:
navigationItem.rightBarButtonItem = UIBarButtonItem(
image: UIImage(named:"Coin"),
style: .Plain,
target:self,
action:"tappedCoinButton:")

Resources