My application uses UITabBarController to present 3 tabs, each tab has a UINavigationController that manag all the view controllers.
What I want to do is to add a view that will be "floating" above all views.
For example suppose I want to show my logo at the top-left corner of the screen, and I want this logo to stay on screen no matter where the user navigates, no matter witch tab he is on.
I suppose I need to add this logo to the UIWindow? I just wonder what is the best practice for doing it?
Dont add anything to the Window other than the navigation controller,or root controller. Trust me you will run into memory issues if you do this as Window isnt released but View Controllers are.
Add that floating view to to each view controller and put it as the last item in the list of subviews. You can design it once in IB or progmatically and duplicate it on each screen. This is the approach i always take and your suggested idea doesn't save you any time.
Indeed, a good place to place it is in the window just when the app launches. E.g.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//...
//... Setup root View Controller
UIView *extraView = [[UIView alloc] initWithFrame:CGRectMake(0,0,10,20)];
[extraView setBackgroundColor: [UIColor greenColor]];
[self.window addSubView: extraView];
return YES;
}
Related
I am in the process of making my first iPhone app with storyboard in Xcode and am stuck. My main screen has a view controller called MemberViewController, which is the home screen. This screen has a UIView that is smaller than the view controller called mainContent.
So basically, I want to be able to load different view controllers (all the same size as the UIView) inside the UIView.
MemberViewController (home page)
-mainContent (UIView)
GetStartedViewController (separate view controller that I want to show inside the UIView)
ProfileViewController (separate view controller that I want to show inside the UIView)
For example, I want the GetStartedViewController to have a button that I can press to switch to the ProfileViewController. The view controllers need to be able to replace each other inside the mainContent UIView.
Thank you so much in advance for your help. If there's an easier way to do this, please let me know.
I use this technique a lot actually. My typical setup of a root UIViewController (you call it memberController) has a view with a UITabBar at the bottom, and then another UIView (you call it mainContent) which contains the rest of the space above that bar.
memberController stays on the screen all the time. Inside of mainContent, add a UINavigationController and initialize it with your first content-carrying GetStartedViewController. When you want to switch tabs on your tab bar, send the appropriate message to this UINavigationController and the views will change inside.
HINT: say your UINavigationController is called navController - you can get rid of the navigation bar (blue one at the top) by sending the message [navController setNavigationBarHidden:TRUE];
EDIT: The code you requested looks like this. This adds a nav controller to a window's view in the applicationDidFinishLaunchingWithOptions method. Instead of window, just do this same thing on your view controller in the viewDidLoad.
window and navController are both properties
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.navController = [[UINavigationController alloc] initWithRootViewController:[YourViewController new]];
[self.navController setNavigationBarHidden:YES];
[self.window addSubview:self.navController.view];
[self.window makeKeyAndVisible];
Hope this helps!
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.
I am working on tabbar application. When application started, by default first tab is selected.
What I want to is when I start application, tab bar should be displayed without selected tab. Like say, if i have 4 tabs then non them get selected when application get launched. By default first one get selected.
I want to display some views which are not part of any of tabs.
Is it possible to do ?
Thanks...
Yes, this is possible.
You need to create a view programmatically and add that view in Window as SuperView , when you don't need it just remove it form SuperView.
[SuperViewname removeFromSuperView];
Code Snippet:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the tab bar controller's view to the window and display.
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
**AdditionalView**
//======================= LoginView ================================================
loginview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
imgview_logingpage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
imgview_logingpage.image=[UIImage imageNamed:#"Screen.jpg"];
loginview.backgroundColor=[UIColor blackColor];
[self.window addSubview:Viewnavigation.view];
[self.window addSubview:loginview]; // To add the View in Window View
}
// To REmove the View from SuperView
-(void)login_clicked:(id)sender
{
Homepage *obj_homepage=[[Homepage alloc]initWithNibName:#"Homepage" bundle:nil];
[self.window addSubview:obj_homepage.view];
[loginview removeFromSuperview];
[loginview release];
}
Or either the more easy way is : Open a new view via PresentModalViewController
If you have a visible tabBarController, then something will necessarily be selected. No way around this.
However, if you would like to hide the tabBar, then you can certainly do that, either by setting its hidden property to YES or by presenting a modal view on top of the selected tab (e.g. the first viewController).
Yes it is possible to display views that are not part of one of the view controllers managed by the tabbar controller. There are many way to do that.
You can present a view controller modally, or just add a subview in the tabbar controller's view.
But as long as the tabbar controller get instanciated, there are no way to deselect every tab.
I need to have a view which shows first before other views but doesn't have a tab bar button.
Is there a way to do this ?
EDIT:
I don't want to show it modally as i want to use to standard function which show other views and having to cater for different ways of showing the view would be messy.
You could add your tabBarController in your window only once you need it and then remove your view from it's superview do discard it and free memory.
Something like:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)showTabBarController {
[window addSubview:tabBarController.view];
[viewController.view removeFromSuperView];
self.viewController = nil;
}
So you want the tab bar to be visible, yet none of its items should be active and the visible view is not a part of the tab bar hierarchy? I don't think this is possible...
And even if you make things look like the described scenario, I have some doubts whether apple will approve an app circumventing standard functionality like this.
You mean UITabBarController?
Try use View Based Application or Navigation Based Application when creating the project in XCode
I have an app that has a login screen and splash screen. I want these screens to show up one after the other, and as such I have resulted to adding them as subviews to the app delegate's window property.
[window addSubview:mainViewController.view];
[window addSubview:loginViewController.view];
[window addSubview:splashScreenViewController.view];
The problem is that each subsequent subview that is added seems to appear in a weird manner(the orientation of the nib file is not obeyed). I know my nib files are not at fault, because if I only include one view, no matter which view it is, it shows up right. Furthermore, if I add the views as subviews to the main view controller's view(as opposed to app delegates) view, i dont get this issue. However, there is a 20px gap at the bottom of the screen because I guess UIWindow accounts for the status bar, and normal UIView's do not.
Does anyone have any idea what the problem might be? Is UIWindow doing something special? Am I not supposed to add several subviews to UIWindow? If anyone has tips regarding how to do splash screens, that would be much appreciated.
I suggest you use a UIView in the middle...
UIView view = [[UIView alloc] init];
[view addSubview:mainViewController.view];
[view addSubview:loginViewController.view];
[view addSubview:splashScreenViewController.view];
[window addSubview:view];
[view release];
After what if you what to animate as a splash screen, take a look at one of my post :
how to open a new uiview like a popup?
You should only have 1 view controller at a time.
You should use the -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController delegate function to check which tab was clicked. If it's your second view (which it should be if there are only 2 tabs) then call a custom method in your splashscreencontroller that pops up the modal. If that splash screen is only supposed to show once, make sure to set a flag afterwards to make sure it doesn't pop up again.
A TabBarController loads all the views before displaying them, so there's no lag when a user switches between tags.