I am trying to replace the image displayed in my right navigation bar button when it is tapped.
At the moment what seems to be happening is that it is not replacing the button, but trying to set it while the old one still exists. This leads to a strange effect as you can see:
It's a compass image, so there should only be one circle, but as you can see three are appearing.
This is my code:
let backImg: UIImage = UIImage(named: "Compass.png")!
navButton.setBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)
Is there a way to remove the existing one and replace it with the new one?
Thanks.
It sounds like perhaps you are setting the UIBarButtonItem's backgroundImage when what you really want to set is its image. Thus you end up seeing both of them.
Related
I wish to change the image of a tab bar item to a new image at runtime subject to a different view being updated.
Is there any way to achieve this?
I've struggled for hours now so any help would be massively appreciated.
Swift 4.2 Xcode 10.1
EDIT:
I've tried setting through self.tabBarController?.tabBar.items?[0].image in viewDidLoad in my VC. I've tried sub classing a TabBarController and setting through there, to which I couldn't get the runtime behaviour I was after.
To be more clear, i'm happy with my current selected and non selected image called "globe", but I want to listen out to a variable called "truth" and when truth is true I want to change the non selected image to "globe_red".
It is that behaviour of changing the image during the apps runtime that I am struggling with!
You can use following code to change image of tabBar.
self.tabBarController?.tabBar.items?[0].image = #imageLiteral(resourceName: "img")
or for selectedImage
self.tabBarController?.tabBar.items?[0].selectedImage = #imageLiteral(resourceName: "img")
Instead of 0 in items, you can use your index for selected tabBar.
Ok I fixed it!
Subclass UITabBarController
In viewDidLoad of your subclass set self.tabBar.items?[0].image and self.tabBar.items?[0].selectedImage to your desired image. Here I am settings the first VC's tab bar item but you can set any with the index or loop through and set all.
So in my iOS app I have a menu designed like this:
The Images are created with the following code:
cell.imageCell?.image = menuItems[indexPath.row].image
cell.imageCell.image = cell.imageCell.image?.withRenderingMode(.alwaysTemplate)
cell.imageCell.tintColor = MenuTableViewController.fontColor
cell.imageCell.backgroundColor = UIColor.clear
This code is placed in the tableview cellForRowAt function. Now all the different viewcontrollers(HomeViewController, InfoViewController etc...) have no access at all to the menu controller and thus are not able to change the color of the image and nowhere else am I changing the color of these images. Now when I press one of the tabs that don't use any Alerts or modal views like home or info, the images stay perfectly fine, however when I press on Weather or excursions which download a json file with URLSession dataTask and display an alert telling to please wait, the images turn grey like the following:
I'm not sure how this is even possible that one viewcontroller can change another viewcontrollers subviews.
Thanks in advance
-Jorge
Perhaps what you see is a dimmed presentation of your icons. Don't use image templates or set tintAdjustmentMode to normal to stop fading icons. Usually that happens if you display alert or modal controller.
It looks like its the default UITableViewCell click style.
Try to set the style to none,
as answered here: How can I disable the UITableView selection highlighting?
Swift:
cell.selectionStyle = UITableViewCellSelectionStyle.none
This is the first time I'm using a UITabBar and I'm having problems getting it looking the way I want to look.
Right now it still has some grey shadow over cast over it. I cannot seem to remove this!
I've added this line to my code but the shadow still keeps showing up:
self.universalTabBar.setValue(true, forKey: "_hidesShadow");
Can anyone tell me what I'm doing wrong?
Try setting the rendering mode of each tab bar item's image.
universalTabBar.tintColor = UIColor.whiteColor()
for tabBarItem in universalTabBar.items! {
tabBarItem.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal)
}
i have a uibutton. im using two images for UIControlStateNormal and UIControlStateSelected. when i touch down the button it shows the UIControlStateSelected image for a split second. what is the reason?
I'm not sure i've got ur question correctly but try setting the image for UIControlStateHighlighted instead of UIControlStateSelected if you want the other image to be displayed as long as you keep the button pressed.
Reference: UIControl Documentiation says,
Note that the control can be in more than one state
In you case Selected and Highlighted. So UIControlStateNormal image is the default image used for highlighted state.
This is my best guess.
Good Luck,
Swapnil
I notice something strange happens to one of my view controller: the back button disappears, yet it's possible to go back to previous view controller by tapping the top left corner (i.e where the button should reside).
In my entire file there's no line that set self.navigationItem.hidesBackButton to YES; also NSLog prints 0 as self.navigationItem.hidesBackButton's value in viewDidLoad.
This occurs in both the simulator and real device. Any ideas?
Oh dear. In the implementation of the previous view controller, I accidentally set self.title to #"", which causes this annoying bug.
Remove this line solves the problem.
I had a recursive navigation controller, and this also happened to me, I used this code to fix it:
self.navigationItem.leftItemsSupplementBackButton = true
Just in case someone is facing this issue with a custom back button and the above fixes did not work, here is a similar issue I faced with a different solution.
I had a customized back button with text that was disappearing while the arrow could be seen UINavigationController custom back button disappears from NavigationBar
So if anyone is facing a similar situation with disappearing back button text on a customized back button, here is my scenario and fix.
I customized my back button inside a custom NavigationController class as follows:
private func customizeBackButton() {
let backImage = UIImage(named: "BackButton")?.withRenderingMode(.alwaysOriginal)
navigationBar.backIndicatorImage = backImage
navigationBar.backIndicatorTransitionMaskImage = backImage
UIBarButtonItem.appearance().setTitleTextAttributes([
NSAttributedString.Key.foregroundColor: UIColor.panoStoryYellow,
NSAttributedString.Key.font: UIFont(name: "Montserrat-SemiBold", size: 15)!
], for: .normal)
}
This gave me:
Now when I tapped on the back button text, the text disappeared:
I made sure that I followed all the above answers such as setting titles making sure the tint color is valid etc. however this did not work.
In my case, I needed to set attributes even for the highlighted state of the back button as follows:
UIBarButtonItem.appearance().setTitleTextAttributes([
NSAttributedString.Key.foregroundColor: UIColor.panoStoryYellow,
NSAttributedString.Key.font: UIFont(name: "Montserrat-SemiBold", size: 15)!
], for: .highlighted)
After this, the back button text never disappeared