swift xcode - navigationitem.rightbarbuttonitem not showing image literal though just appearing blue - swift

just showing blue and not image literal
My rightBarButtonItem is just showing blue and not the image literal?
NavigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "Contacts Book"), style: .plain, target: self, action: #selector(handleNewMessage))

I think it's safe to assume this isn't a png with a transparent background.
barButtonItems take pngs and fill every existing pixel with default color blue.
You can change the color, but I assume you don't want any other block in place of the blue one.
You can either find a way to make this not a bar button item or you can edit your image to take the background out.
Something like this:
Works better than something like this:

You need to set property of your image that is below:
Step1:Go to Assets.xcssets
Step2:Select your imageset
Step3:Go to property inspector
Step4:In imageset you need to set Original Image into Render as property.

it worked by doing this
NavigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named:"Grape")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleNewMessage))
now it shows the actual image and not just a complete blue image

Related

back button title color not changed while we open popover or present form controller in iOS 13

In iOS 13 back button title color not dimmed whenever we open popover or present other controller. Other part of application grayed out but not back button.
also tintColorDidChange function is not available for UIBarButtonItem so is there any way to do grayed out all back button titles.
Note: Back button image is being grayed.
Have you try by changing tint color of bar button?
I got same problem and solved by using
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.clear]
self.navigationController?.navigationBar.tintColor = .red // put your desire color
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

UIAlertController does not turn all elements in the background to B/W

EDIT: Repository with my solution: UIAlertControllerDimmed
After showing UIAlertController, most of the background gets 'dimmed' and turns black and white. Some elements get darker, but don't turn B/W.
These elements are (from top to bottom on the screenshot):
UIImageView inside UINavigationItem
UIButton with red background color and white image
UIImageView inside UITabBarItem
I couldn't find anything related to this topic. What do I have to change to also get these items dimmed?
Here is the without the UIAlertController:
]
I think what's going on here is that you're setting the tintColor of some of the elements and you get different behavior for tintColor than you do for backgroundColor or textColor (or the colors in an image).
When an alert or action sheet appears, iOS 7 automatically dims the
tint color of the views behind it. To respond to this color change, a
custom view subclass that uses tintColor in its rendering should
override tintColorDidChange to refresh the rendering when appropriate.
For example, I created a simple app that displays an alert controller. I set the left button tint color to clear color and the text color to blue:
I set the right button tint color to system green color:
When I run the app and present the alert controller it looks like this
Before:
After:
In order to get the behavior you're looking for, you'll need to follow the advice in #Alexander's answer. You'll need to create grayscale versions of the four images on the screen and animate the transition to them.
You can have a helper function to animate the color change
fileprivate func dimElements(highlight: Bool) {
UIView.animate(withDuration: 0.3) {
self.sendButton.backgroundColor = highlight ? .red : .gray
}
}
and then call it when presenting/dismissing the alert.
let alert = UIAlertController(title: "Error", message: "Oops!", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .cancel, handler: {_ in self.dimElements(highlight: true) })
alert.addAction(okAction)
self.dimElements(highlight: false)
present(alert, animated: true, completion: nil)
Thank you for your help.
In order to have a more flexible solution I decided to create a subclass of UIAlertController which captures a screenshot, turns it to grayscale colors and inserts it behind the UIAlertController when it gets presented. This way it works without having to do any additional work, and you don't need to implement fade animations for every single element that does not turn to grayscale colors by default.
Github repo: UIAlertControllerDimmed

How do I get back button effect (<)

I am writing an app , where I want custom back effect of the default OS. Like the system which is giving with Settings ( <)
< Icon
Refer this image (I want the effect like Settings with the <)
I already have the functionality, I just want the look and feel.
Effect what I am getting.
I have tried the following
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "cancel")
Also, I have tried adding it via Storyboard. How do I get it ?
Use the backBarButtonItem and you will get right effect.
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "cancel")
Also, you make sure
self.navigationItem.backBarButtonItem will be shown only after your have pushed another one view to navigation stack, controlled by self.navigationController, if no left button on navigation bar is displayed.

Swift: Custom Nav Bar Image not firing action on click

I have a navigation bar on my viewcontroller.
I have created a custom image (of a cog) and have got that to show right:
//Add bar item
var image = UIImage(named: "settingsIcon")
image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
NavigationBar.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: nil, action: "addTapped")
I added the action to the above code:
action: "addTapped"
Then I created a function:
func addTapped() {
print("Tapped")
}
When I run the app, the image shows fine. When I click the image nothing happens, not even an error.
Am I doing something blatantly wrong here?
Try setting the target to self instead of nil.

How can I set image location correctly in Swift?

In my app, I am using SWRevealViewController. Currently everything is working OK except for the menu image position.I set up the menu button programatically. Now I need to put it to the correct place.It also needs to work in all screen sizes and needs to be at the same place.
I set up the menu button like this
override func viewDidAppear(animated: Bool) {
let image = UIImage(named: "reveal-icon#2x.png")
tabBarController?.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style:
UIBarButtonItemStyle.Plain, target: self.revealViewController(), action: Selector("revealToggle:"))
tabBarController?.navigationItem.leftBarButtonItem?.image = image
}
and here is the result
How do I fix it? I think I need to do it programatically...
And here is the overall layout of my storyboard file.
Thanks for your answers.
This might be happening because you are initializing the UIBarButtonItem with a title. Initialize it with something like this:
let img = UIImage(named: "reveal-icon#2x.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let leftBarButtonItem = UIBarButtonItem(image: img, style: UIBarButtonItemStyle.Plain, target: self.revealViewController(), action: Selector("revealToggle:"))
tabBarController?.navigationItem.leftBarButtonItem = leftBarButtonItem