Objective-C: Back Button disappear after present/push ViewController from AppsDelegate - ios

The pic below is my storyboard and I wish to present the Upcoming VC at AppDelegate. Below is my code at AppDelegate I manage to present the Upcoming VC when called.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc=[sb instantiateViewControllerWithIdentifier:#"Upcoming"];
UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:vc];
nv.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.window.rootViewController presentViewController:nv animated:YES completion:nil];
At the Upcoming VC, I manage to present the navigationBar with title and background colour but my back button < remain missing. My back button is auto generated.
-(void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBar.hidden = NO;
[self.navigationItem setHidesBackButton:FALSE];
//====Make the navigation Bar appear===
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = nil;
//=== Set the navigation Back < color
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[self.navigationController.navigationBar setBarTintColor : [ UIColor grayColor]];
//=== Set the navigation Bar text color
[self.navigationController.navigationBar
setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
self.navigationController.navigationBar.translucent = NO;
}
Please help.

What do you expect then ?
back button will be only visible when you push your UpcomingVC from existing ViewController
remember that just taking navigation bar in storyboard will now show backbutton automatically.
You can create full view controller hierarchy
Like
UpcomingVC is pushed from HomeVC then you can set HomeVC as root view controller embedded in UINavigationController and to the next line you can push
UpcomingVC with animation false

you can tyr this
StoryBoard to StoryBoard
ViewControllerName * next = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerName"];
[self.navigationController pushViewController:next animated:YES];
you must be nib registered.

Related

How to toggle view controllers using a UITabBar?

My goal for this project is to cycle between View Controllers using a UITabBar, without a UITabBarController, because according to Apple docs, TabBarControllers should not be pushed to UINavigationControllers, which this project uses already.
So far, I am using this UITabBar from #samuel's answer from this question. How to add UITabBar in iphone using objective c
//viewDidLoad
...
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 431, 320, 50)];
[self.view addSubview:tabBar];
[[UITabBar appearance] setBarTintColor:[UIColor blackColor]];
NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Home" image:[UIImage imageNamed:#""] tag:0];
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:#"Rules" image:[UIImage imageNamed:#""] tag:1];
[tabBarItems addObject:tabBarItem];
[tabBarItems addObject:tabBarItem1];
tabBar.items = tabBarItems;
tabBar.selectedItem = [tabBarItems objectAtIndex:0];
}
I am ready to cycle between View Controllers when I press the tabBarItems. What is the correct way to do this? I want to go to a View Controller named ViewController9 when the Rules tabBarItem is tapped, and then to ViewController6 when the Home tabBarItem is tapped.
Can someone please share some code? Here's what I've tried but nothing happens when the tabBarItems are pressed:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSInteger selectedTag = tabBar.selectedItem.tag;
NSLog(#"%ld",(long)selectedTag);
if (selectedTag == 0) {
//Do what ever you want here
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"ViewController9"];
[self presentViewController:vc animated:YES completion:nil];
} else if(selectedTag == 1) {
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"ViewController9"];
[self presentViewController:vc animated:YES completion:nil];
} else { //if(selectedTag == 2)
//Do what ever you want here
}
}
How to push a view controller using a UITabBar using Objective C?
That question doesn't make much sense if you think about it. "Push" is a stack operation, and a navigation controller manages a stack of view controllers. UITabBar isn't a view controller at all, and it doesn't manage a stack of anything, so pushing isn't an operation that's available.
My goal for this project is to cycle between View Controllers using a UITabBar, without a UITabBarController, because according to Apple docs, TabBarControllers should not be pushed to UINavigationControllers, which this project uses already.
There's a reason for that, and the reason is that navigation controllers and tab controllers offer different modes of controlling what the user sees on the screen. If a tab controller could be part of a navigation stack, the user would see the tab bar appear and disappear, when it's intended to be something that's fixed. It would create a very confusing interface.
If you use a tab bar alone to get around the fact that Cocoa Touch discourages you from creating a confusing user interface, you're going to end up with a confusing user interface. Think hard about what you're really trying to do, and if you decide you still need to do it, consider using UI elements that don't look like a tab bar so that the user doesn't expect tab bar behavior.

Status bar color does not match navigation bar

I have a view controller I am presenting modally. I want the status bar color to match the navigation bar color.
I have set UIViewControllerBasedStatusBarAppearance to YES because I don't want this change across the entire application.
I am setting self.navigationController.navigationBar.barTintColor but this is only changing the navigation bar color. The status bar remains a lighter color.
I have tried various combinations of setNeedsStatusBarAppearanceUpdate and preferredStatusBarStyle but none have any effect.
View controller is launched like so:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:searchController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navigationController animated:YES completion:nil];
My steps are below, check where you go wrong:
Create 2 view controller like below, you can let vc1 embed in a navigation controller:
In the vc1, you drag a action of the button:
Code is below:
- (IBAction)clickAction:(UIButton *)sender {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController2 *searchController = [sb instantiateViewControllerWithIdentifier:#"ViewController2"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:searchController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navigationController animated:YES completion:nil];
}
Then in the vc2, you set the title:

Add semi-transparant background viewcontroller with search bar throughout app

need help in adding search bar ViewController which whose SearchBar will be in NaviagationBar with Back button (navigation search i achieved - self.navigationItem.titleView = searchBarView) throughout app, but until it has search i want to show previous ViewController in background with semi-transparent black color just like i achieved in Android :
i can add semi-transparent ViewController to current ViewController :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
[vc setTransitioningDelegate:transitionController];
vc.modalPresentationStyle= UIModalPresentationCurrentContext;
[self presentViewController:vc animated:YES completion:nil];
but what it is doing that it opens SecondViewController in transparent without NavigationBar, the FirstViewController has NavigationBar
And when the SecondViewController is opened it should have SearchBar in NavigationBar and it shouldn't be Transparent as i achieved in Android.
There will be n number of ViewController which will add this same controller as Overlay Controller with NavigationBar and back button.
Please Help.
I found some solutions which involve taking a snapshot of the view and adding them to your navBar controller.
From Apple:
https://developer.apple.com/library/ios/qa/qa1817/_index.html
Other interesting option:
iOS iPhone is it possible to clone UIView and have it draw itself to two UIViews?
In you code snippet you just create new SecondViewController and present it like modal. It appears without navigation bar because you create it without Navigation Controller.
If you want to keep SecondViewController in the same navigation stack as previous ViewController with navigation bar and default Back button you should call:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
[self presentViewController:vc animated:YES completion:nil];
[self.navigationController pushViewController:vc animated:YES];
To make SecondViewController like semitransparent, take screenshot of previous view controller, pass this image to SecondViewController and use it like background. You can apply this image to additional ImageView on your SecondViewController view or just call:
self.view.backgroundColor = [UIColor colorWithPatternImage:self.backgroundImage];

load page with pushViewController on tabbar

I have UIViewController with tabbar botton, and navigation bar and navigation item,
when I press on my button in navigation Item I want to load a view, I don't know how to load this view on tabbar,
would you please help me,thanks in advance!
-(IBAction) infoPage:(id)sender
{
InfoCtrol *i = [[InfoCtrol alloc] initWithNibName:#"InfoCtrol" bundle:nil];
[self.navigationController pushViewController:i animated:YES];
}
This will load the InfoCtrol controller and set the Touch UpInside Event in xib to the infoPage method
Do you mean you want to hide the tab bar when you push new controller in navigation?
If true. There is a property hidesBottomBarWhenPushed in the UIViewController class.
// Instead of adding ViewController to TabbarController, add NavigationControllers.
// Eg.
UINavigationController *NavController = [[UINavigationController alloc] initWithRootViewController:_viewCtrl1];
[watchListNavController.navigationBar setTintColor:[UIColor blackColor]];
_tabC = [[UITabBarController alloc] init];
_tabCt.viewControllers = [NSArray arrayWithObjects:NavController,_viewCtrl2,_viewCtrl3, nil];
// Now you can use Push and Pop In your _viewCtrl1.
// Do same with all the viewController

Black Translucent Navigation Bar Flashes Opaque on First Push

I have a Navigation Controller where I set the navigationBar.barStyle = UIBarStyleBlack and the navigationBar.translucent = YES (as per Apple's advice since they deprecated UIBarStyleBlackTranslucent). In my two nib files (this is not using storyboard) in the simulated metrics I have Top Bar set to Black Navigation Bar.
SettingsViewController *controller = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
controller.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(closeSettings)];
controller.navigationController.navigationBar.barStyle = UIBarStyleBlack;
controller.navigationController.navigationBar.translucent = YES;
[self presentViewController:navController animated:YES completion:nil];
When I present the navigation controller, it opens up with the black translucent bar properly, but when I then push to the next table view, the navigation bar quickly fades to opaque and then back to translucent in the course of about 200ms. Its pretty much flashing opaque then back to translucent.
When I then push to the next table view, or go back (either by pressing the button in the top left of the nav bar, or by popping the view) it doesn't flash. It stays translucent the whole way through until the whole navigation controller is dismissed.
I thought this might be because of the way the nib was set up with an opaque bar, but I have tried every type of option (translucent, the regular blue, no bar) and it still does it.
Also, it does this across both completely separate navigation controllers in my app. Sorry if I am doing something obviously wrong, but I have tried so many combinations of options and am just at a loss.
Thanks!
I think you shouldn't use BarStyle at all but just :
[controller.navigationController.navigationBar setTintColor:[UIColor blackColor]];
[controller.navigationController.navigationBar setTranslucent:YES];
I would also try to animate the style change to get a smoother one with that in the presented view controller viewDidAppear method (think to begin with a black opaque bar in your code):
...
// Keep that where you want
SettingsViewController *controller = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
controller.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(closeSettings)];
// remove the two lines
[self presentViewController:navController animated:YES completion:nil];
}
Then in the SettingViewController code put :
- (void) viewDidAppear:(BOOL)animated {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
[controller.navigationController.navigationBar setTintColor:[UIColor blackColor]];
[controller.navigationController.navigationBar setTranslucent:YES];
[UIView commitAnimations];
}
Set appropriate value to your UIViewController instance property edgesForExtendedLayout, and set backgroundColor to your navigationController, for example:
self.edgesForExtendedLayout = UIRectEdgeBottom;
self.navigationController.view.backgroundColor = [UIColor whiteColor];
Hope this can help you.
SWIFT 5 VERSION
self.edgesForExtendedLayout = UIRectEdge.bottom
self.view.backgroundColor = .white

Resources