UINavigationController TitleView - Need clarification - objective-c

Okay, there's one thing I really don't understand.
I have a navigation controller (created in AppDelegate.m) as the first item in a tab bar controller (created in AppDelegate.h):
self.tabBarController.viewControllers = #[tabOneNavigationController, viewController2, viewController3, viewController4, viewController5];
In another class, I access this tabOneNavigationController by:
AppDelegate *apd = (AppDelegate *) [[UIApplication sharedApplication] delegate];
UINavigationController *navtab1 = [apd.tabBarController.viewControllers objectAtIndex:0];
And if I want to change the background of navtab1's navigation bar, I write:
[navtab1.navigationBar setBackgroundImage:navigationBackgroundImage forBarMetrics:UIBarMetricsDefault];
But for changing titleView, I've only seen examples using:
self.navigationItem.titleView = ...
but how do I do change navtab1's titleView?
Example: I have a custom TableViewCell, which contains a button, and when that button's clicked, it should change the titeView of navtab1 (in this case, self obviously doesn't have navigationItem property.

Here is use delegate methode of UINavigationController, This code might be helpful in your case:
#pragma mark -
#pragma mark - UINavigationController Delegate Methods
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
UINavigationBar *morenavbar = navigationController.navigationBar;
UINavigationItem *morenavitem = morenavbar.topItem;
morenavitem.titleView = myCustomeView; // Add Here your custom UIView.
morenavitem.rightBarButtonItem.tintColor = [UIColor blackColor];
}
In above Method you also put image/color of NavigationBar.

Related

back button doesn't show up

I push a ViewController on my UINavigationController, but I see no 'back' button... the NavBar is empty but there.
In my storyboard, my initial View Controller is the NavigationController, and I set the 'Top Bar' to 'None' in the Simulated Metrics section. When I switch to Inferred or Navigation Bar, everything work ok, I see the back button with text "back". (but then all my View are ugly since I removed the NavBar for a reason).
How could I preserve my design (NavBar hidden/not there) and be able to push a new ViewController with a NavBar and Back button. NavBar on demand!
Thanks
Found a solution: just put my AppDelegate as the UINavigationController delegate and manually hide/show the NavBar.
In my Storyboard, the navigation controller is with:
Simulated Metrics -> Top Bar -> None
Navigation Controller -> Bar Visibility -> uncheck "Shows Navigation
Bar"
The AppDelegate .h/.m:
#interface AppDelegate : UIResponder <UIApplicationDelegate, UINavigationControllerDelegate>
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (something) {
navigationController.navigationBarHidden = TRUE;
}
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (somethingElse) {
navigationController.navigationBarHidden = FALSE;
}
}
You want to configure navigation in application
try this in ApplicationDidFinishLoading Method in App delegate
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[window makeKeyAndVisible];
[window addSubview:navController.view];
after use this to change view in action method of button
DetailViewController *ViewController = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushNavigationController:ViewController animated:YES];

iOS UISplitViewController's Popover controller button disappear after pushing new view controller in portrait mode

In my UISplitViewController application, I have
RootViewController - view controller in the left pane.
DetailViewController - view controller in the right pane.
When one item (which is in a UITableView) in RootViewController is tapped, new view controller will be set as the following shows:
[detailViewController setViewControllers:[NSArray arrayWithObjects:newViewController, nil] animated:animated];
//detailPane is my DetailViewController
All works pretty well in landscape mode. However, I can't make the UISplitViewController work as what I want in portrait mode, that is, the RootViewController's popover button does not appear appropriately in my DetailViewController when I launch and use the application in portait mode.
When I launch the app in portrait mode, the popover button appears appropriately. But after tapping one item in the popover and a new view controller has been set on detailViewController, the button disappeared. I have to rotate the device to landscape and then back to portrait again to make the button appear again.
I set my UISplitViewController's delegate in my application's AppDelegate as follows:
self.splitViewController.delegate = self.detailViewController
And here is my UISplitViewControllerDelegate implementation
- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {
NSLog(#"Will hide view controller");
barButtonItem.title = #"Menu";
[self.navigationItem setLeftBarButtonItem:barButtonItem];
self.popoverController = pc;
}
- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
NSLog(#"Will show view controller")
NSMutableArray *items = [self.navigationItem.leftBarButtonItems mutableCopy];
[items removeAllObjects];
[self.navigationItem setLeftBarButtonItems:items animated:YES];
[items release];
self.popoverController = nil;
}
Any hint or help is greatly appreciated.
Thanks.
Just came up with a new solution.
Subclass UINavigationController and implement UISplitViewControllerDelegate. Set an instance of this class as the right ViewController of the splitViewController. Everytime you want to change the detail view controller from the master
NewDetailViewController *newDetailVC = ....// Obtain the new detail VC
newDetailVC.navigationItem.leftBarButtonItem = [[[[self.splitViewController.viewControllers objectAtIndex:1]topViewController]navigationItem ]leftBarButtonItem]; //With this you tet a pointer to the button from the first detail VC but from the new detail VC
[[self.navigationController.splitViewController.viewControllers objectAtIndex:1]setViewControllers:[NSArray arrayWithObject:newDetailVC]]; //Now you set the new detail VC as the only VC in the array of VCs of the subclassed navigation controller which is the right VC of the split view Controller
This works for me and I can avoid defining a hole protocol and setting the master as the delegate, which is a big trade off. Hope it helps.
If you still need it:
http://developer.apple.com/library/ios/#samplecode/MultipleDetailViews/Introduction/Intro.html
What I did to my source (I had similar setup to you) to fix it:
I have the master viewcontroller (UITableViewController in my case) be the delegate of the UISplitViewController. In the two delegate methods for UISplitViewControllers (so this would be in your master viewcontroller implementation) you would save the popupviewcontroller and the barbuttonitem in your class. Now, if you change your details viewcontroller, you do:
self.viewControllers = [NSArray arrayWithObjects:[self.viewControllers objectAtIndex:0], newDetailsViewController, nil];
UIViewController <SubstitutableDetailViewController>*vc = (UIViewController <SubstitutableDetailViewController>*)newDetailsViewController;
[vc invalidateRootPopoverButtonItem:_tableViewController.rootPopoverButtonItem];
[_createReportViewController showRootPopoverButtonItem:_tableViewController.rootPopoverButtonItem];
where we have
#protocol SubstitutableDetailViewController
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
#end
the delegate that each of your detailsViewControllers should adhere to. You would implement like this:
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
self.navigationItem.leftBarButtonItem = barButtonItem;
}
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
self.navigationItem.leftBarButtonItem = nil;
}
Let me know if this helps you.
I liked Nekto's solution, but it misses one key problem.
It's not clear what action: selector will cause the UISplitViewController to show the MasterViewController in a popover. When I finally figured this out, by examining the BarButtonItem in the debugger, I realized why it was so tricky to figure this out: the action: selector isn't documented anywhere in Apple's iOS SDK. Oops.
Try this:
UIBarButtonItem *showListView = [[UIBarButtonItem alloc] initWithTitle:#"List" style:UIBarButtonItemStyleBordered target:[self splitViewController] action:#selector(toggleMasterVisible:)];
[[detailViewController navigationItem] setLeftBarButtonItem:showListView];
You may want to surround this code with a conditional that checks the window is in in portrait mode, such as if ([self interfaceOrientation] == UIInterfaceOrientationPortrait)
When you are setting new view controllers placed on navigation stack, probably, all navigation buttons are reset. You can manually add appropriate buttons after changing navigation stack.
For example, you can pick code from - (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc where default popover controller button is created:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:#"Menu" style:UIBarButtonItemStyleBordered target:self action:#selector(appropriateSelector)];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
self.popoverController = pc;

Change color of UIDocumentInteractionController nav bar

Is there a way to change the tint/background color of UIDocumentInteractionController navigationbar?
A cleaner version of #DOOManics implementation:
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return [self navigationController];
}
If you put the UIDocumentInteractionController onto a UINavigationController it will automatically take the color its navbar. This is probably your root view navcontroller.
You do this with the documentInteractionControllerViewControllerForPreview method:
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller
{
// Use the rootViewController here so that the preview is pushed onto the navbar stack
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
return appDelegate.window.rootViewController;
}
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:107.0/256.0 green:145.0/256.0 blue:35.0/256.0 alpha:1.0]];
Place this code in Appdelegate's didFinisLaunching method. It will change the color of the navigation bar for the whole app.
Try this code:
- (void)openEC:(NSURL*)url {
[UINavigationBar appearance].tintColor = [UIColor blueColor];
docController = [UIDocumentInteractionController interactionControllerWithURL:url];
[docController setDelegate:self];
[docController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
- (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller {
[UINavigationBar appearance].tintColor = [UIColor whiteColor];
}
Swift version to #dvdfrddsgn implementation
Try this : (You need to implement UIDocumentInteractionControllerDelegate)
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self.navigationController ?? self
}
If you're not using a navigationController, you can set the navigation bar color in the UIDocumentInteractionController by setting the correct settings on the View of the UIViewController where you launch the UIDocumentInteractionController from.
Let's say you have UIViewController viewController1 (from somewhere here you launch the UIDocumentInteractionController), with a View1 in the storyboard.
With the Storyboard open, click on the View1 from the list of elements on the viewController1 and go to "Attributes inspectors" on the right side. The Background and the Tint set there will be used in your UIDocumentInteractionController as well afterwards.
Then you can just use:
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}
Note that inside the viewController1, you might have a Navigation Bar with different properties, and these will not be used in the UIDocumentInteractionController.

Back button not appearing on UINavigationController

I have a UINavigationController setup in my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the navigation controller's view to the window and display.
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
In my RootViewController I am pushing another view onto the stack:
//Show the deals
DealViewController *dvc = [[DealViewController alloc] initWithNibName:#"DealViewController" bundle:nil];
[self.navigationController.navigationBar setHidden:NO];
[self.navigationController pushViewController:dvc animated:YES];
The view shows up, but there is no back button that is added to my navigation bar. Why is this and how can I resolve it?
Are you setting self.title in RootViewController? Perhaps the UINavigationController doesn't have any text to put on the back button, so it omits it...?
Are you setting hidesBackButton = YES or backBarButtonItem = nil in DealViewController, or does it have a different leftBarButtonItem defined?
Try this:
DetailViewController *detailViewController = [[DetailViewController alloc] init];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle : #"Back"
style : UIBarButtonItemStyleDone
target : nil
action : nil];
self.navigationItem.backBarButtonItem = back;
[self.navigationController pushViewController : detailViewController animated : YES];
[detailViewController release];
You must think of the navigation controller as a stack of navigation controllers each controlling one screen full of information.
You instantiate the navigation controller with the
-(id)initWithRootViewController:(UIViewController *)rootViewController
method. You specify the root view controller in this call. Then you add the navigation controller's view as a subview to the window, like you did before.
If you want to show your second screen you push another view controller on the stack by using
-(void)pushViewController:detailViewController animated:YES
method.
Using presentModalViewController to show the naviagtionController. Set the navagitionController bar button like so:
[navigationController.navigationBar.topItem setLeftBarButtonItem:
[[[UIBarButtonItem alloc] initWithTitle: #"Back"
style: UIBarButtonItemStylePlain
target: self
action: #selector(dismisstheModal:)] autorelease]];
This happened to me because in my navigation controller's content controller I had set up some navigation controller behavior in viewDidLoad and in another class that inherits from my content controller, and the one that was being presented, i implemented a viewDidLoad as well, and forgot to call [super viewDidLoad] which thereby caused me to override the base class's viewDidLoad where I was setting up my navigation controller buttons. Oooops.

Can't show a nagivationbar after hiding it

I have the following in the mainwindow.xib
Navigation Controller
List item
Tab bar controller
tabbar
firstViewController
SecondViewController
The entrypoint
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
navController.viewControllers = [NSArray arrayWithObject:tabBarController];
[window addSubview:navController.view];
[window makeKeyAndVisible];
return YES;
}
and now in the first viewcontroller i'm writing
-(void)loadView
{
if(rootAppDelegate==nil)
rootAppDelegate=(tabbarAppDelegate *) [[UIApplication sharedApplication]delegate];
listEmergencyItems= rootAppDelegate.listOfEmergencySectionItems;
self.rootAppDelegate.navController.navigationBarHidden = NO;
[super loadView];
}
and in the second viewcontroller i'm writing
- (void)loadView
{
if(rootAppDelegate==nil){
rootAppDelegate=(tabbarAppDelegate *) [[UIApplication sharedApplication]delegate];
}
listHospitalsItems= self.rootAppDelegate.listOfHospitalsItems;
self.rootAppDelegate.navController.navigationBarHidden = YES;
[super loadView];
}
And on the runtime, when it first loads the first view, i see the navigationbar where i need to navigate into a detail view.
And when i press the second tab bar item, i go to the second view, and the navigation bar gets hidden.
But when i press back on the first tabbar item, i.e. returning to the first viewcontroller. the navigation bar remains hidden.
Any idea?
The navigation bar won't show in the second view because neither view was placed on the navigationcontroller's stack. you want to use something like this in the parent to present a child view instead of overriding loadView
ViewToPresentViewController *myVController = [[ViewToPresentViewController alloc] initWithNibName:#"ViewToPresentViewController"
bundle:nil];
myVController.property = someValue;
[self.navigationController pushViewController:myVController
animated:YES];
[myVController release];
then, as i said previously, you can just use [self.navigationController setNavigationBarHidden:animated:]