UITabBar over UIViewController - ios

I have a UITabBarController with two UIViewController inside.
How to initilaiser UIViewController not to display a portion is below the UITabBar ?
Edit:
AppDelegate.m:
ViewController *viewController = [[ViewController alloc] init];
self.tabController = [[UITabBarController alloc] init];
[self.tabController setViewControllers:#[viewController] animated:YES];
self.window.rootViewController = self.tabController;
ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview: [[MyGraphicView alloc] initWithFrame:[self.view bounds]]];
}

How to initilaiser UIViewController not to display a portion is below the UITabBar ?
I think you're mistaking the gloss effect for translucency that's allowing the table to show through the tab bar. Here's an image showing two tabs:
It does look like the content view is showing through from under the tab bar, but it's not really showing through -- you're just seeing the glossy effect used to draw the tab items. To illustrate, I'll change the background color of the content to purple:
If the content were really showing through the tab bar, you'd see purple showing instead of the white/gray that you can see is there. To see this in your own app, just scroll the table a bit. If the table were really showing through the tab bar, you would be able to read the content of the next cell, including the "9" and the separator line. I'm sure you won't see that, though.

Related

iOS - Is it still possible to dynamically add items to UITabBarController in this age of adaptive layout?

I have an app that communicates with an outside entities. I need to either show or hide an item on the tab bar if that entity has a switch flipped on or off.
It has to be dynamic in case the user changes from entity A to entity B and those have different settings. Tab bar item needs to show/hide if they switch and settings differ.
I used to add an item to a UITabBarController like this:
UIViewController *vc1 = [[MyViewController1 alloc] init];
[self.tabBarController addChildViewController:vc1];
UIViewController *vc2 = [[MyViewController2 alloc] init];
[self.tabBarController addChildViewController:vc2];
And I could remove items from a tab bar like this:
NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
[tbViewControllers removeObjectAtIndex:4];
[tbViewControllers removeObjectAtIndex:4];
[self.tabBarController setViewControllers:tbViewControllers];
In my ViewController Code I had something like this:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
//get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
//give it a label
[tbi setTitle:#"Tab Item"];
//create a UIImage from a file
UIImage *i = [UIImage imageNamed:#"image.png"];
//put that image on the tab bar item
[tbi setImage:i];
}
return self;
}
Now, with the advent of adaptive app layout and segues it seems that adding an item dynamically does not work. At least for me.
I get a black screen instead of the view I expect to see when doing the above. The UIView in question is basically a UIWebView with a couple custom buttons above it.
My app is fully adaptive layout compatible with UIStackViews and whatnot.
All the posts I see here referencing adding or removing items from UITabBars are all several years old, before modern adaptive layout.
Is it still possible to add/remove tab items from the tab bar anymore? Is it advisable to do so or is this now bad practice?
Any other ideas to accomplish this same goal of having an item on a tab bar or not depending on an outside setting?
Cheers,
TJ
I found my own solution and am sharing it to help others.
The trick was how the UIViewController was created.
The new way to do this is to create the view controller by instantiating from the storyboard.
I found the idea from this question.
Creating and adding to the TabView is done like this:
// Get a reference to the Tab View Controllers array
NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
// Get a reference to the storyboard
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
// Instantiate a new instance of the ViewController
UIViewController *vc1 = [sb instantiateViewControllerWithIdentifier:#"MyVC"];
// Add the new view controller to the array at the desired location
[tbViewControllers insertObject:vc1 atIndex:tbViewControllers.count-1];
// Set the tab bar controllers to the newly augmented array
[self.tabBarController setViewControllers:tbViewControllers];
That's it. The view controller works as I expect it to. No more black screen.
Hope this helps someone else.

Pushing view onto viewcontroller in tab controller

So I'm trying to convert a single view application into a tabbed application. My use case is this - in one of the view controllers I want to push a new view controller and still have the tab underneath.
I'm currently doing this -
[self.tabBarController setViewControllers:#[self.searchViewController, self.loginViewController]];
[self.searchViewController presentViewController:self.searchViewController.detailController animated:YES completion:nil];
However, this makes the tab across the bottom disappear.
What should I do?
presentViewController is a "modal" presentation - the presented view controller takes over the entire screen. If you want to remain within the tab but move between view controllers, the root view controller in the tab should be a UINavigationController. You can then push/pop view controllers onto that.
There are two primary methods for view navigation, the first is a presentation which displays a view from the bottom that slides up, and the second is a push which displays a view from the right that slides in from the side.
In most cases, the view I am going to display and what action kicked off the navigation determine which method I will use. For example, if I have a table view that has a list of music albums and I want to search for a song by a particular artist, to see the songs within that album I want to PUSH the view controller, i.e. slide to the right. This gives me the built-in (and intuitive) ability to go back via the automatically added back button on the navigation bar in case the song I was looking for wasn't in the album I selected.
If perhaps I wanted to present the user with the ability to edit the album details, such as renaming the album, this is a totally different type of action, and I would want to PRESENT such a view modally, i.e. from the bottom.
The major distinction between the two is where are you going and what are you doing. If the next view you are going to show is something that does one action and you are then returned back to the original view, presenting modally from the bottom is conventional. If you are doing to be potentially navigation further and further into subsections and will be coming back and forth between said subsections, like Artist->Album->Song etc., you are going to want to push the view from the side, just like the default music app in iOS does.
This is an example starter project I created that demonstrates an easy way to make this work the way you likely want. I create instances of the different view controllers I want to be contained in the tabBarController, which are associated with the tabs, and then "wrap" them each with their own navigation controller before adding them to the tabBar via the .items property. This way each view controller has its own navigation hierarchy and within each you'll be able to call [self.navigationController pushViewController:] or [self.navigationController presentViewController] to keep the navigation 'within' the views and separate from the tabBar itself.
#import "AppDelegate.h"
#import "TabBarViewController.h"
#import "InfoViewController.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
InfoViewController *firstVC = [[InfoViewController alloc] init];
firstVC.title = #"First";
firstVC.view.backgroundColor = [UIColor redColor];
UINavigationController *firstNC = [[UINavigationController alloc] initWithRootViewController:firstVC];
InfoViewController *secondVC = [[InfoViewController alloc] init];
secondVC.title = #"Second";
secondVC.view.backgroundColor = [UIColor blueColor];
UINavigationController *secondNC = [[UINavigationController alloc] initWithRootViewController:secondVC];
TabBarViewController *tabBarVC = [[TabBarViewController alloc] init];
tabBarVC.viewControllers = #[firstNC, secondNC];
self.window.rootViewController = tabBarVC;
[self.window makeKeyAndVisible];
return YES;
}
That resulted in the following:
Hope that helps!

How to get Navigation bar inside UIPopover Controller

I am using a uipopover controller with navigation bar.Actually i want to have navigation bar inside the popover controller. i tried changing the background color of the navigation controller but it didnt look as i expected.The navigation bar buttons seems to be attached with the popover corners.Please find the screenshot attached.Below is my code which i used to create the popover.
WLViewBookmarkViewController * viewBookmarkViewController = [[WLViewBookmarkViewController alloc] initWithStyle:UITableViewStyleGrouped];
viewBookmarkViewController.delegate = self;
UINavigationController *viewBookmarkNavController = [[UINavigationController alloc] initWithRootViewController:viewBookmarkViewController];
viewBookmarkNavController.navigationBar.backgroundColor = [UIColor colorWithRed:9/255.0 green:135/255.0 blue:46/255.0 alpha:1.0];
self.bookmarkPopoverController = [[UIPopoverController alloc] initWithContentViewController:viewBookmarkNavController];
[self.bookmarkPopoverController presentPopoverFromBarButtonItem:bookmarkButtonItem permittedArrowDirections:UIPopoverArrowDirectionDown|UIPopoverArrowDirectionUp
animated:YES];
Please suggest if there are any other way to achieve this.
I would init the UIPopover controller with the UINavigationController directly
self.bookmarkPopoverController = [[UIPopoverController alloc] initWithContentViewController: viewBookmarkNavController];
Also try settings the content size of the popover by overriding -contentSizeForViewInPopover in the viewBookmarkViewController class to return a CGSize, you should get neater results this way.
I created a Xib in where i had a view for the controller which i had to add inside the popover.i added a table view and a navigation bar into the view.

How to use UISplitViewController

I'm trying to use a split view controller to show a navigation controller on the left and a table view on the right. I use this code in RootViewController's viewDidLoad:
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
Settings *settings = [[Settings alloc] init]; //Table view
MainView *main = [[MainView alloc] init]; //Table view
UINavigationController *nav_con = [[UINavigationController alloc] init];
NSArray *controllers = [[NSArray alloc] initWithObjects:settings, detailViewController, nil];
[nav_con pushViewController:settings animated:NO];
self.view = nav_con.view;
detailViewController.view = main.view;
I've tried like a million different ways of coding this, and this one comes the closest to correct. It displays the navigation controller in the left pane and the main view in the right. HOWEVER, in the left pane, at the top, there are two bars with a big black space between them. One of the bars in my nav controller's bar. How can I just replace the content of the left pane entirely with my navigation controller's view?
Settings and MainView better be subclasses of UITableViewController
the first object in controllers should be nav_con, not settings
delete the last two "view" lines,
and RootViewController should be a subclass of UISplitViewController and the instance that's being created should be set to window.rootViewController somewhere.
Also, it's fairly standard to do all this code external to viewDidLoad - makes me wonder what's being loaded as the view! Much easier to do all this in a nib file.

iPad - multiple UIBarButtonItem

I have UINavigationController and I`ve placed UIListView in it. Now I want to add multiple BarButtons to left side of navigation bar. How is that possible? I managed to add one button there via code but not multiple.
Edit: Buttons added via IB to NavigationBar of UINavigationController aren`t visible at all. What could cause the problem?
I`ve created UINavigationController in .h file and used this in .m and pushed another view (that TableView):
navigationController = [[UINavigationController alloc] init];
[window addSubview:[navigationController view]];
tableOfContents *tableOfContentsViewController = [[tableOfContents alloc] init];
[navigationController pushViewController:tableOfContentsViewController animated:NO];
[tableOfContentsViewController release];
Edit 2: I solved second problem. So the first question only remains. Multiple BarButtonItems ...
iOS 5.0 has apis to do this. Check the following properties of UINavigationItem Class
leftBarButtonItems
rightBarButtonItems
leftItemsSupplementBackButton
The only way you can do this is to add the UIBarButtonItem to a UIToolBar and make a UIBarButtonItem with the UIToolBar as the customView.
There are many examples on the web, check out:
http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html

Resources