Transparent UINavigationBar - ios

Am newbie to ios and I found this solution on making the UINavigationBar Transparent.
Where in my project files can I put this code
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
So that it is applied in my entire project where navigation controller is being used.

Put in your viewDidLoad function of your rootViewController this code:
Objective-C:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
Swift 2.x:
if let navigationBar = navigationController?.navigationBar {
navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
navigationBar.shadowImage = UIImage()
navigationBar.translucent = true
navigationController?.view.backgroundColor = .clearColor()
}
Swift 3:
if let navigationBar = navigationController?.navigationBar {
navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationBar.shadowImage = UIImage()
navigationBar.isTranslucent = true
navigationController?.view?.backgroundColor = .clear
}
This works for sure! Transparent UINavigationBar.

If you want change the appearance for all your application, I recommend you to use this:
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
hope that will help

In your UIViewController class. You can also use UIAppearance mechanism http://nshipster.com/uiappearance/
And place this
[[UINavigationBar appearance] setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
[UINavigationBar appearance].shadowImage = [UIImage imageNamed:#"Your image file here"];
into
- ( BOOL ) application:( UIApplication* ) application didFinishLaunchingWithOptions:( NSDictionary* ) launchOptions

Transparent UIToolbar:
self.toolbar.setBackgroundImage(UIImage(),
forToolbarPosition: UIBarPosition.Any,
barMetrics: UIBarMetrics.Default)
self.toolbar.setShadowImage(UIImage(),
forToolbarPosition: UIBarPosition.Any)
Transparent UINavigationBar:
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true

set following code
Self.navigationcontroller.navigationbar.transculant=yes;

Related

Show original image for selected UITabBarItem (storyboard) iOS10

I am using UITabBarController with 5 ViewController each ViewController have own UINavigationController as below:
I have added tabar image like
As seen from above image when TabBarItem is selected just blue image i can see...i wanted to show original image for selected tabaritem.
I had seen the example of setting property UIImageRenderingModeAlwaysOriginal. but i can not see anything on story board.
Below is the way I push TabBarController:
HomeViewController *vcHome = (HomeViewController*)[[UIStoryboard storyboardWithName:#"NexTabBar" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"ID_HOME_VC"];
vcHome.delegate = self;
[self.navigationController pushViewController:vcHome animated:YES];
How can I use the property UIImageRenderingModeAlwaysOriginal here so that I can see original images when particular TabBarItem is selected?
or if any other way is there with ios10.
Here is how I create tabBarItem for myTabBar
let storyboard = UIStoryboard(name: storyboardIdentifier, bundle: nil)
let vc:UIViewController? = storyboard.instantiateInitialViewController();
let selectedImage = UIImage(named: imageName)
let notSelectedImage = UIImage(named:selectedImageName)?.withRenderingMode(.alwaysOriginal)
let item = UITabBarItem(title: title, image: notSelectedImage, selectedImage: selectedImage)
vc!.tabBarItem = item
Objective C version:
UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboardIdentifier bundle:nil];
UIViewController* vc = sb.instantiateInitialViewController;
UIImage* selectedImage = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage* notSelectedimage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem* item = [[UITabBarItem alloc]initWithTitle:title image:notSelectedimage selectedImage:selectedImage];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
vc.tabBarItem = item;
return vc;
If this did not help try to change also tabBar tints colors
self.tabBar.barTintColor = .orange
// set color of selected icons and text to white
self.tabBar.tintColor = .white
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .selected)
// set color of unselected text to gray
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.gray], for: .normal)

Use only custom image for navigationItem backBarButton Item

I want to remove the title and use only custom image for back button in Navigation Controller.
I came across questions like this and I have tried this:
navigationItem.backBarButtonItem = UIBarButtonItem(image: UIImage(named: Constants.Image.kClose), style: .plain, target: nil, action: nil)
I am putting this in viewDidLoad() of the View Controller which is pushing a UITabbarController. ( I know its not a very good idea to push Tab Bar in a Navigation Controller, but I need to do this).
I am getting the following result:
But I don't want that default blue back button. I just want my custom close button to appear there. How can I achieve this?
Create a bar button
- (UIBarButtonItem *)backButton {
UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 4, 34, 34)];
leftButton.layer.cornerRadius = leftButton.frame.size.width / 2;
leftButton.layer.masksToBounds = YES;
[leftButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[leftButton addTarget:self action:#selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
return leftBarButton;
}
and set like this
self.navigationItem.leftBarButtonItem = [self backButton];
self.navigationController?.navigationBar.backIndicatorImage = backImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage
self.navigationController?.navigationBar.backItem?.title = ""
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: UIControl.State.highlighted)

How to Set Colored Customizable Image on Navigation Bar Back Button?

I done the following code for customizing the navigation bar back button. but I am facing the problem on setting the original image. Whatever the color of bar tint, same color is applied to that image also. But i want my original image on the Navigation Bar in Objective-C.
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
UIImage *image = [UIImage imageNamed:#"logoHeader"];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:nil];
self.navigationItem.leftBarButtonItem = backButton;
Use imageWithRenderingMode and get the original image.
UIImage *image = [[UIImage imageNamed:#"logoHeader"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:nil];
self.navigationItem.leftBarButtonItem = backButton;
This code 100% working in Swift iOS (for back button custom image set)
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.backgroundColor = .white
self.navigationItem.title = "Nitish"
let backBtn = UIBarButtonItem(image: UIImage(systemName: "arrow.backward"), style: .plain, target: self, action: #selector(tap))
self.navigationItem.hidesBackButton = true
self.navigationItem.leftBarButtonItem = backBtn
}
#objc func tap() {
print("Tap button Clicked")
}

In UIImagePickerController Cancel button not showing?

here i m using below code. please help me if anybody know this issue.
and i tried below urls also, but its not working. please help me
iOS7 UIImagePickerController cancel button disappear
UIImagePickerController inside UIPopoverController doesn't show Cancel button on iOS7
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
picker.navigationBar.tintColor = [UIColor redColor];
picker.navigationBar.barStyle = UIBarStyleBlackOpaque;
picker.navigationBar.topItem.rightBarButtonItem.tintColor = [UIColor blackColor];
My Issue:
My Req:
This is what worked for me:
present(imagePicker, animated: true, completion: {
imagePicker.navigationBar.topItem?.rightBarButtonItem?.tintColor = .black
})
Looks like apple made some mistake with it (iOS 10, Xcode 8) because just changing tint color of UIImagePickerController could not be done, cause, before controller isn't have topItem property, or navigationController property. So have done the changes in UIImagePickerController extension. But I checked navigationController and topItem in those overrided methods: viewDidLoad, viewWillAppear, viewDidAppear. but it still was nil. So i decide to check it in viewWillLayoutSubviews, and voila! It's wasn't nil, so we can set bar tint color of exact rightBarButtomItem here!
Here is example:
extension UIImagePickerController {
open override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.black
self.navigationBar.topItem?.rightBarButtonItem?.isEnabled = true
}
}
And don't forget to call super.viewWillLayoutSubviews, it's very important ;-)
EDIT: But it still has problems when return to the albums screen..
If every effort doesn't work then you must have to use appearance() method. So try to write following lines in any view controller(UIImagePickerController) which you want to present. You can use appearance in any class but you may have used UIBarButtonItem.appearance() for some other purposes so find that and write that code in viewDidDisappear().
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .highlighted)
Swift 4.2 Solution, Add below code into AppDelegate's didFinishLaunchingWithOptions
UINavigationBar.appearance(whenContainedInInstancesOf: [UIImagePickerController.self]).tintColor = .black
I used the same code that you posted. Its showing the "cancel" button.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
picker.navigationBar.tintColor = [UIColor redColor];
picker.navigationBar.barStyle = UIBarStyleBlackOpaque;
picker.navigationBar.topItem.rightBarButtonItem.tintColor = [UIColor blackColor];
[self presentViewController:picker animated:YES completion:NULL];
May be you are using any custom navigation bar and you set the property for app delegate to changing the tint color of navigation nar.
I think you have not embedded viewController to Navigation Controller
Please do that and try the code: (Objective - c)
-(void)viewDidLoad
{
[super viewDidLoad];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:^{
}];
UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 300)];
imageview.image=image;
}
Seems like update some of item properties make the button visible, so... just turn it enabled like that:
present(imagePicker, animated: true, completion: {
self.imagePicker.navigationBar.topItem?.rightBarButtonItem?.isEnabled = true
})
If any of the above does not work out for you,
try adding this when you define your UIImagePickerController
imagePicker.modalPresentationStyle = .popover // Or.fullScreen (Can also check cases for iPad and iPhone as per requirement)
Worked for me like a charm. Was quite bugged up by this
Subclass UIPickerViewController as below:
class CustomImagePicker: UIImagePickerController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UINavigationBar.appearance().tintColor = UIColor.black
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.init(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)], for: .normal)
}
override func viewWillDisappear(_ animated: Bool) {
UINavigationBar.appearance().tintColor = UIColor.white // your color
UIBarButtonItem.appearance().setTitleTextAttributes(nil, for: .normal)
super.viewWillDisappear(animated)
}
}
And use As:
func openGallary()
{
picker!.sourceType = UIImagePickerController.SourceType.photoLibrary
picker!.modalPresentationStyle = .currentContext
self.present(picker!, animated: true, completion: nil)
}
change the UIImagePickerController navigationBar.tintColor
try this code :
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor blueColor]];
UIImagePickerController *imgPicker=[[UIImagePickerController alloc]init];
imgPicker.delegate = self;
imgPicker.navigationBar.barStyle = UIBarStyleBlackOpaque;
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPicker.allowsEditing = NO;
if([UIImagePickerController isSourceTypeAvailable: sourceType])
{
imgPicker.sourceType=sourceType;
[self presentViewController:imgPicker animated:YES completion:NULL];
}
This worked for me:
let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: .blue), NSFontAttributeName: UIFont.boldSystemFont(ofSize: 16.0)], for: .normal)
I have face this kind of same issue and after wasting lot of time i found solution. I have customise navigation bar appearance like below code that why this issue was happened.
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "ic_left")
let attributes = [NSAttributedString.Key.font: UIFont(name: "FuturaPT-Medium", size: 16)!]
UINavigationBar.appearance().titleTextAttributes = attributes
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "line.png"),for: .bottom,barMetrics: .default)
UINavigationBar.appearance().shadowImage = UIImage(named: "line.png")
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:
UIColor.clear], for: .normal)
This is just custom appearance of navigation bar we just need to change last line of code because i have set title color to clear. This is really very simple. Put this code before presenting your image picker controller.
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:
UIColor.black], for: .normal)
And again if you don't want navigation bar button title make text color to clear.
Thanks
Please try this code for swift 3 or above
let picker = UIImagePickerController()
picker.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.white
Swift 5:
imagePicker.modalPresentationStyle = .fullScreen
Try this Code:
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
And add delegate Method: - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:^{
}];
self.img = image;
[self.iconBtn setBackgroundImage:image forState:UIControlStateNormal];
}
i think this will help you.Just paste the code inside imagePickerControllerDidCancel then dismiss it (Swift 4).
picker.setNavigationBarHidden(false, animated: true)

Restore navigationBar background image after setting it to [UIImage new]

I needed a completely transparent navigation bar for the mapView so I did this:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
That returns the desired effect, as seen here:
Now I have a problem when I go to any other because my navigationBar remains transparent:
How do I restore default settings of the navigationBar's backgroundImage and shadowImage?
Set nil for image of navigation Controller on viewWillDisappear on map view
Set this two method in your mapview
MapView.m
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
}
-(void)viewWillDisappear:(BOOL)animated{
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:nil];
}
For Swift 3:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = nil
}
by the way, you get get the original background image by using function
UIImageView *imageView = [self.navigationController.navigationBar
backgroundImageForBarMetrics:UIBarMetricsDefault];
and store the image somewhere, then you use
[self.navigationController.navigationBar setBackgroundImage:imageView
forBarMetrics:UIBarMetricsDefault];
to set it back,but most time, set to nil will solve your problem.

Resources