I want to give title in UIImagePickerController as shown in image screen shot
Set UIImagePickerController delegate to self and implement the following method of UINavigationControllerDelegate:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController.navigationItem setTitle:#"<Your-Title>"];
}
You try this code:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UINavigationItem *ipcNavBarTopItem;
// add done button to right side of nav bar
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Photos"
style:UIBarButtonItemStylePlain
target:self
action:#selector(saveImages:)];
UINavigationBar *bar = navigationController.navigationBar;
[bar setHidden:NO];
ipcNavBarTopItem = bar.topItem;
ipcNavBarTopItem.title = #"Photos";
ipcNavBarTopItem.rightBarButtonItem = doneButton;
}
You need to implement the delegate method for UINavigationController, in Swift:
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
viewController.navigationItem.title = "Some title"
}
Related
I have open photo gallery for choosing video. Here is my code :
- (void)selectMediaFrom:(UIImagePickerControllerSourceType)sourceType {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = sourceType;
imagePicker.delegate = self;
//imagePicker.allowsEditing = YES;
if (selectedMediaType == MediaVideos) {
imagePicker.mediaTypes = #[(NSString *)kUTTypeMovie, (NSString *)kUTTypeVideo];
if (sourceType == UIImagePickerControllerSourceTypeCamera) {
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
}
}
else if (selectedMediaType == MediaPhotos) {
imagePicker.mediaTypes = #[(NSString *)kUTTypeImage];
if (sourceType == UIImagePickerControllerSourceTypeCamera) {
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
}
}
if (IS_IPHONE) {
[self presentViewController:imagePicker animated:TRUE completion:nil];
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self setModalPresentationStyle:UIModalPresentationFormSheet];
[self presentViewController:imagePicker animated:TRUE completion:nil];
});
}
}
And it is always showing title of UIImagePickerController as Photos, See screenshot I have attached.
But, I have open iPhone default Photos app and check, that shows videos folder : See below
Then why it is not showing Videos title ? Can we change it ?
Yes You can change the title of Navigation which you want.
Add the delegate of UINavigationControllerDelegate
Swift 3.0
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool){
viewController.navigationItem.title = "Kirit Modi"
}
Objective C
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
[viewController.navigationItem setTitle:#"Kirit Modi"];
}
See Output :
Thanks for this. I was able to adapt for my situation. Because I present my ImagePicker programmatically from another VC, I had to use similar code.
I first created a variable for the presenting VC:
let imagePickerController = UIImagePickerController()
And then later in the presenting VC, I used:
Swift 4
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
// change title here to something other than default "Photo"
imagePickerController.navigationBar.topItem?.title = "choose photo"
}
It works well. Hope this helps someone.
I have created a UIViewController with a UITabbar in it.
I did not use UITabbarController because I wanted UITabbar on the top of the screen.
Upon clicking tab1, I want to present controller1 and on clicking tab2 I want to present controller 2. I don't want the tabbar to hide. I want to display the controller beneath the tabbar.
#interface MTLeaderFactoViewController () <UITabBarDelegate>
#property (weak, nonatomic) IBOutlet UITabBar *tabBar;
#end
#implementation MTLeaderFactoViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
if (item.tag == 0) {
NSLog(#"item tag 0");
} else {
NSLog(#"item tag 1");
}
}
#end
My questions:
1) didSelectItem method is not triggered even after using UITabbarDelegate
2) What is the most elegant way of displaying the controller when clicked on a button? I don't want to use segue as all the controllers are in different storyboards.
For now, I plan to do
Controller1 *fp = [Controller1 controllerStoryboard:STORYBOARD_COURSE];
[self addChildViewController:fp];
[self.view addSubview:fp.view];
[fp didMoveToParentViewController:self];
EDIT 1:
Controller1 *fp = [Controller1 controllerStoryboard:STORYBOARD_COURSE];
[self addChildViewController:fp];
[self.view addSubview:fp.view];
[fp didMoveToParentViewController:self];
Tried this but it hides the tab bar. I want to utilize the space beneath the tab bar to display the controller
What you need to do is have a basecontroller class which will contain a tabbar(programatically created) then you can achive the desired output heres a sample baseController that i created,
import UIKit
class BaseViewController: UIViewController,UITabBarDelegate{
override func viewDidLoad() {
super.viewDidLoad()
let myTabBar = UITabBar()
myTabBar.frame = CGRect(x: 0, y: 60, width:self.view.frame.size.width, height: 50)
let one = UITabBarItem()
one.title = "one"
one.tag = 1
let two = UITabBarItem()
two.title = "two"
two.tag = 2
myTabBar.setItems([one,two], animated: false)
self.view.addSubview(myTabBar)
myTabBar.delegate = self
}
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
switch item.tag {
case 1:
let controller = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
addChildViewController(controller!)
view.addSubview((controller?.view)!)
controller?.didMove(toParentViewController: self)
break
case 2:
let controller = storyboard?.instantiateViewController(withIdentifier: "ViewController")
addChildViewController(controller!)
view.addSubview((controller?.view)!)
controller?.didMove(toParentViewController: self)
break
default:
break
}
}
}
View Controller class :
import UIKit
class ViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
SecondView Controller :
import UIKit
class SecondViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
I would suggest using UITabBarController instead of UIViewController.
Add UITabViewController in your StoryBoard name it FirstTabBarController.
Add child view controller to FirstTabBarController. In all your child view of add UITabbar. Connect the TabBar delegate to each of your child ViewController.
Hide the default TabBar in your default FirstTabBarController.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.tabBar.isHidden = true;
[self setSelectedIndex:1];
}
Add action to TabBar in your child ViewController as
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSUInteger index = [[theTabBar items] indexOfObject:item];
NSLog(#"Tab index = %u", (int)indexO);
[self.navigationController.tabBarController setSelectedIndex:index];
}
I would prefer this method over manually adding adding or removing ViewController as subview, let the UITabBarController manage that for you. Do let me know if you have further queries.
When I present the ImagePickerController the statusBar text color is still black, how to make like this?
Just three steps:
1: Add UINavigationControllerDelegate,UIImagePickerControllerDelegate to your
#interface yourController ()<>
2: imagePickerController.delegate = self;
3:
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
Swift solution by writing an extension for UIImagePickerController:
extension UIImagePickerController {
convenience init(navigationBarStyle: UIBarStyle) {
self.init()
self.navigationBar.barStyle = navigationBarStyle
}
}
Then you can set the color when initializing it:
let picker = UIImagePickerController(navigationBarStyle: .black) // black bar -> white text
Alternative (inspired by folse's answer): When you initialize the UIImagePickerController normally, make this class the delegate (picker.delegate = self) and implement this function:
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
if navigationController is UIImagePickerController { // check just to be safe
navigationController.navigationBar.barStyle = .black // black bar -> white text
}
}
In Swift and iOS 9, setStatusBarStyle is deprecated. You could subclass the controller.
private final class LightStatusImagePickerController: UIImagePickerController {
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .lightContent
}
}
I had the same problem having to manage the application runned under different iOS versions.
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if(IS_IOS8_AND_UP) {
imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;
} else {
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
}
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
The, in delegate:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
/* Cancel button color */
_imagePicker.navigationBar.tintColor = <custom_color>
/* Status bar color */
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
Using the answers above the following worked for me:
Implement
UINavigationControllerDelegate, UIImagePickerControllerDelegate to your UIViewController
and set
imagePickerController.delegate = self;
Add the following method:
-(void) navigationController: (UINavigationController *) navigationController willShowViewController: (UIViewController *) viewController animated: (BOOL) animated {
navigationController.navigationBar.barStyle = UIBarStyleBlack;
}
I was facing a similar problem and I found the cleanest way to solve it was to override preferredStatusBarStyle in an extension of UIImagePickerController like so. This principal can be applied to third party libraries nicely.
extension UIImagePickerController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
if isLightTheme() {
return .default // black text
}
return .lightContent // white text
}
}
isLightTheme() is simply a function to determine whether the NavigationBar in that controller is a light or dark colour.
This is the quickest solution I could think of. Create the following category:
#implementation UIImagePickerController (LightStatusBar)
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
#end
After going through every single stackoverflow solution for this problem, it's still frustratingly not working for me.
//UIBarButtonItem declaration
UIBarButtonItem* button1 = [[UIBarButtonItem alloc] initWithTitle:#"Button Text"
style:UIBarButtonItemStyleBordered target:self action:#selector(myAction)];
//method 1
[self setToolbarItems:[NSArray arrayWithObjects: button1, nil] animated:YES];
//method 2
[self.navigationController.toolbar setItems:[NSArray arrayWithObject:button1]];
//method 3
self.navigationController.toolbarItems = [NSArray arrayWithObject:button1];
//displaying toolbar
[self.navigationController setToolbarHidden:NO];
None of the above methods work for displaying a button on the toolbar - all I get is a blank toolbar. Is there something obvious I'm missing here?
Move
//UIBarButtonItem declaration
UIBarButtonItem* button1 = [[UIBarButtonItem alloc] initWithTitle:#"Button Text"
style:UIBarButtonItemStyleBordered target:self action:#selector(myAction)];
//method 1
[self setToolbarItems:[NSArray arrayWithObjects: button1, nil] animated:YES];
//displaying toolbar
[self.navigationController setToolbarHidden:NO];
to viewDidAppear:(BOOL)animated this is the point where UINavigationController get toolbar items of UIViewController that it manages.
use
self.toolbarItems=[NSArray arrayWithObject:button1]
With Swift 3 / iOS 10, in the simplest case where your navigation controller will contain only one view controller, you may use the code below in order to display your view controller with a toolbar that contains a bar button item:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Show navigation controller’s built-in toolbar
navigationController?.setToolbarHidden(false, animated: false)
// Set the view controller toolbar items
let items = [UIBarButtonItem(title: "Button Text", style: .plain, target: nil, action: nil)]
setToolbarItems(items, animated: false)
}
}
However, if you plan to have several view controllers in your navigation controller's stack, you will have to call UINavigationController's setToolbarHidden(_:animated:) method in viewWillAppear() and viewWillDisappear() in order to properly show or hide the navigation controller’s built-in toolbar:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Set the view controller toolbar items
let items = [UIBarButtonItem(title: "Button Text", style: .plain, target: nil, action: nil)]
setToolbarItems(items, animated: false)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Show navigation controller’s built-in toolbar
navigationController?.setToolbarHidden(false, animated: false)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Hide navigation controller’s built-in toolbar
navigationController?.setToolbarHidden(true, animated: false)
}
}
For those looking for a Swift version, try this:
let someVC: UIViewController = ...
let someButton: UIBarButtonItem = ...
someVC.setToolbarItems([someButton], animated: true)
The UINavigationController.toolbar property documentation explicitly clarifies which API should be used for setting toolbar items:
Management of this toolbar’s contents is done through the custom view controllers associated with this navigation controller. For each view controller on the navigation stack, you can assign a custom set of toolbar items using the setToolbarItems:animated: method of UIViewController.
-- UINavigationController Class Reference
I have two UITableViewControllers such that when I click next on the first UITableViewController, the second UITableViewController gets pushed on the navigation stack and animates the transition like normal. I'd like to make it so when I push next, only the views animate, and the navigation bar doesn't (stays the same). I've gotten very close to doing this with the code below:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CATransition *navTransition = [CATransition animation];
navTransition.duration = .5;
navTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
navTransition.type = kCATransitionPush;
navTransition.subtype = kCATransitionPush;
[self.navigationController.navigationBar.layer addAnimation:navTransition forKey:nil];
}
I put this code, and I also make it so the title and buttons on both navigation bars are exactly same in each UITableViewController. It almost works, problem is, the navigation bar blinks when the animation occurs. Is there anyway to get it to not blink, or is there any other good way to prevent the animation of the navbar from occurring (ie disabling the animation on the layer or something)?
UPDATE: Anyone got any other ideas? Still struggling with this.
This is what I came up with. Here is the code for the first viewController in the sequence:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (viewController == self)
{
if (self.isInitialized)
{
CATransition *navigationBarAnimation = [CATransition animation];
navigationBarAnimation.duration = 1.5;
navigationBarAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];;
navigationBarAnimation.type = kCATransitionFade;
navigationBarAnimation.subtype = kCATransitionFade;
navigationBarAnimation.removedOnCompletion = YES;
[self.navigationController.navigationBar.layer addAnimation:navigationBarAnimation forKey:nil];
}
else
{
self.isInitialized = YES;
}
}
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (viewController == self)
{
if (self.isInitialized)
{
[self.navigationController.navigationBar.layer removeAllAnimations];
}
}
}
Here is the code for the 2nd view controller:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (viewController == self)
{
if (!self.isInitialized)
{
CATransition *navigationBarAnimation = [CATransition animation];
navigationBarAnimation.duration = 1.5;
navigationBarAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];;
navigationBarAnimation.type = kCATransitionFade;
navigationBarAnimation.subtype = kCATransitionFade;
navigationBarAnimation.removedOnCompletion = YES;
[self.navigationController.navigationBar.layer addAnimation:navigationBarAnimation forKey:nil];
self.isInitialized = YES;
}
}
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (viewController == self)
{
if (self.isInitialized)
{
[self.navigationController.navigationBar.layer removeAllAnimations];
}
}
}
You have to use the UINavigationController delegate methods to figure out when the UIViewController is being shown. Then for each UIViewController, need to make a BOOL isInitialized property so it helps you determine when the UIViewController is being pushed on the stack, or when it's being shown because you pushed back on the next UIViewController.
This may not be the best answer/idea but you could just mask the UINavigationBar during the animation.
Create a UINavigationBar that looks exactly the same way as your current UNavigationBar add it to the keyWindow just before the transition takes place, then after it is finished remove it. This will essentially cover just the UINavigationBar and hide its animation.
Swift
Here is solution in swift
var isInitialized = false
Your FirstViewController:
func navigationController(_ navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
if viewController == self {
if self.isInitialized {
var navigationBarAnimation = CATransition()
navigationBarAnimation.duration = 1.5
navigationBarAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
navigationBarAnimation.type = kCATransitionFade
navigationBarAnimation.subtype = kCATransitionFade
navigationBarAnimation.removedOnCompletion = true
self.navigationController?.navigationBar?.layer?.addAnimation(navigationBarAnimation, forKey: nil)
}
else
{
self.isInitialized = true;
}
}
}
func navigationController(_ navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
if viewController == self {
if self.isInitialized {
self.navigationController?.navigationBar?.layer?.removeAllAnimations()
}
}
}
Your SecondViewController:
func navigationController(_ navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
if viewController == self {
if !self.isInitialized {
var navigationBarAnimation = CATransition()
navigationBarAnimation.duration = 1.5
navigationBarAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
navigationBarAnimation.type = kCATransitionFade
navigationBarAnimation.subtype = kCATransitionFade
navigationBarAnimation.removedOnCompletion = true
self.navigationController?.navigationBar?.layer?.addAnimation(navigationBarAnimation, forKey: nil)
self.isInitialized = true;
}
}
}
func navigationController(_ navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
if viewController == self {
if self.isInitialized {
self.navigationController?.navigationBar?.layer?.removeAllAnimations()
}
}
}