How to change initial tab bar selection programmatically - iphone

Having problems changing the initial tab bar selection on an application (i.e. the middle tab is selected on app launch rather than the leftmost tab). The app uses storyboards and the tab bar controller was added later on in development via the storyboard method.
tabBarController.selectedIndex = 1;
the above code doesn't work (because I don't have a custom view controller connected to my tab bar, just the default UITabBarController):
Done some googling and looked at many different resources and haven't quite found a solution for an app that wasn't initially created using Apple's template Tab Bar Application.

Since this is the initial view controller, and is not a subclass, you need to set this in your appDelegate.
In AppDelegate.m, add the following to your application:didFinishLaunchingWithOptions: method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Select the left-most tab of our initial tab bar controller:
UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
tabBar.selectedIndex = 0;
return YES;
}

tabBar setSelectedItem: try using this in your viewDidLoad

Related

No Navigation bar iOS7+ app

What is the best way to build an app without navigation bar ?
I'm going to work on an app from scratch which includes about few navigation but there is no navigation bar. Definitely there will be navigation controller.
I know we can hide navigation bar by setting hidden property as YES. Is there any best recommended approach to build such kind of app ?
Go to your plist and do the below
one more thing on add the below line
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[_navigationController setNavigationBarHidden:YES animated:NO];
}
Just be careful not to place the view contents below the status bar (that is if you don't hide the status bar) and to place a back button somewhere to manually pop view controllers.

The tab bar is shifted down outside the borders of the simulator

I have created an empty project (XCode 4.2 IOS SDK 5), I have added a new view to it and make it the root view controller inside the appDelegate, so when the app runs, it displays that view correctly, however, I added a tabbarcontroller to the view, created an IBOutlet for it inside the newly created view, and added this line to viewDidLoad method of the view:
[self.view addSubview:self.tabController.view];
so the tab bar loads correctly in the iphone simulator, but with a little problem that I couldn't fix: half of that tab bar is shifted down the simulator which prevents tabs' titles from appearing, exactly as in the following screenshot:
How can I fix this problem?
Most likely it's because of status bar. But, because subview where you inserting controller can be any size, most universal solution is:
[tabController.view setFrame:self.view.bounds];
(assuming self.view - is the view, where you adding it)
The view with the tab bar on is 480px high, but the view you're adding it to is smaller than that because of the status bar. This means that it starts 22px too low, and ends 22px too low - off the bottom of the screen.
If you want the tab bar to be global to the app link it to an IBOutlet on the app delegate, then do this in your didFinishLaunching method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
// Add the tab bar controller's view to the window and display.
[window addSubview:tabController.view];
[window makeKeyAndVisible];
return YES;
}
This adds it to the main window, rather than another view. This will anchor it to the top of the screen, so the bottom will be at the bottom of the screen.

Any "non TabBar-template" based tutorial on how to add a UITabBar?

I would like to add a TabBar to an existing view-based application I already started just to allow the user to switch to other parts of the app like the "About" section and another section entitled "Saved Searches" to display a navigational content (saved searches list > specific search result > product details).
Any idea on how to do this ? All tutorials I found point me directly to a TabBar template.
Thx for helping,
Stephane
You could start off with the UITabBar Application Template and you'll realize it's very easy to do:
In your UIApplicationDelegate class, in the method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Instantiate a UITabBarController like this:
UITabBarController *tabBar = [[UITabBarController alloc] init];
Then you set the view Controllers that will appear on the tab bar:
tabBar.viewControllers = viewControllers;
Which is a NSArray you can previously create with your UIViewController subclasses:
NSArray *viewControllers = [[[NSArray alloc] initWithObjects:vc1, vc2, vc3, nil] autorelease];
After this, you only have to set it as the root view controller of the window, or add it as a subview (it has the same effect, but the first approach doesnt work prior to iOS 4)
self.window.rootViewController = tabBar;
or
[self.window addSubView:tabBar.view];
And then
[tabBar release];
To achieve the kind of navigation that you say in your question, the view controllers you set to the tabBar should be instances of UINavigationController, which are very easy to create like this:
UINavigationController *vc1 = [[UINavigationController alloc] initWithRootViewController:firstViewControllerPage];
And inside them, you can push (navigate to) other view controllers doing:
[self.navigationController pushNavigationController:anotherViewController animated:YES];
Hope this brief review of it makes it a bit clear :)
You can create a new UITabBarController, and add it's view as a subview of your applications window. Then, add your other view controllers (for your "About" and "Saved Searches" sections) to that tab bar controller.
This can be done most easily in Interface Builder. In your MainWindow.xib, drag a Tab Bar Controller object onto the canvas. This will automatically create a tab bar with two items (one for each of the view controllers added). For each view controller under the tab bar controller, go to the identity inspector and change its class to your custom view controller subclass. Then, show the attributes inspector and there is a field "NIB Name" - again, set this to the appropriate nib name. Your custom controller views will then be loaded from their corresponding nib files. All that's left to do is name each tab in Interface Builder, and give it a graphic.
You can also do this programmatically if you don't like IB, by assigning the custom view controllers to the tab controller's viewControllers property, and assign a selectedViewController.
Hope this helps.
EDIT
Thought it might be helpful to show a little hierarchy! Your MainWindox.xib structure might look something like this:
AppDelegate
UIWindow
UITabBarController
UITabBar
AboutViewController (view loaded from "AboutViewController.xib")
Tab Bar Item - About
UINavigationController
Navigation Bar
SavedSearchesViewController - Root View Controller (view loaded from "SavedSearchesViewController.xib"
Tab Bar Item
And push appropriate view controllers from SavedSearchesViewController as normal to provide navigation content.

View position is too high

(In the application start up event, I added my main view to app's Window in the following codes:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
MyViewController* vc = [[MyViewController alloc]
initWithNibName:#"MyViewController_iPhone"
bundle:nil];
[window addSubview:[vc view]];
[vc release];
[window makeKeyAndVisible];
return YES;
}
In my MainWindow.xib, there is no view there. It is just an empty window. MyViewController contains a navigation bar and a table. In IB, both main and my view controller has status bar at the top and my view is filled to its max view.
The issue I have is that half of my navigation bar is covered by status bar (in simulator):
I could adjust my view navigation bar lower a bit, but I don't think that's the right way to do it. Not sure if there is any way in IB or in codes to let the added view to Window is filled with the consideration of status bar?
By the way, I am using XCode 3.2.4 and iPhone 3.2 for my app.
Are you animating in the status bar? Chances are, the view is created before the status bar is unhidden, so the view's frame is setup showing full screen. Later, when the status bar is animated in, the view is not automatically resized.

Show/Hide TabBarController in iphone

i am making an application in iphone in which i have 4 tabbars & in one of its tab i have 4 views in 2nd view it needs to hide the tab bar. I am able to hide the tab bar using the setHidesBottomBarWhenPushed:YES in in the initWithNib method of the Viewcontroller being pushed. But when navigating to the screen 3 , calling the same method with "NO" does not make the tab bar appear. any ideas?
John Smith is correct. The URL for that sample is: http://developer.apple.com/iphone/library/samplecode/TheElements/index.html
The code that does this is in AtomicElementViewController.m, and the line that achieves this effect is in the init method:
self.hidesBottomBarWhenPushed = YES;
I had the same issue to show or hide tab bar controller with UITableViewController customized class. Somehow, by using the following codes, does not work to hide tab bar controller:
- (void) viewDidLoad {
self.hidesBottomBarWhenPushed = YES;
}
In the case of storyboard with segue, initWithStyle: method does not get called.
Instead, I have to overwrite the property to make it work:
- (BOOL) hidesBottomBarWhenPushed {
return YES;
}
My case is for iOS 5.1 with storyboard and segue to push to the next view (where I want to hide tab bar controller).
Take a look at Apple's Elements projects. They hide and unhide the tab-bar when you view and individual element.
Before you push your 3rd view onto the stack, set the 2nd view's hidesBottomBarWhenPushed to NO.