troubles adding splash screen subviews to UIwindow - iphone

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.

Related

iOS Overlay view which always sits on top?

I haven't been able to find anything on this and feel it likely isn't possible in a simple manner.
Can I have a view, such a loading bar for example which constantly sits over every other view controller until I choose to dismiss it but at the same time any underlying view can still be interacted with? Sort of acting like a system view. Be persistent when presenting new view controllers and all.
Thanks.
Add it as a subview of your window. Like this:
UIView *myView = ...
[self.window addSubview:myView];
Rather than adding it to the window, as #JackyBoy suggests, add it to the window's rootViewController's view. That will rotate along with the device. If you just add it to the window, you may have problems with rotation.
UIView *myView = ...
[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:myView];

Can two UIViewControllers use a single navigation bar?

In my iPhone app I am trying to have the allusion of having a single static navigation bar, so the title and buttons don't ever swipe across when switching views.
I can't think a way of doing it (simply at least), so do you have any suggestions? I need to have a static title and buttons up in the nav bar space (even if I don't use the UINavigationBar, but make something custom) so that when I do something such as push a view controller, when it swipes across my nav bar doesn't move and the buttons change function for the new view.
Edit
Ok, I have thought of a possible method. Each of my views have a secondary view in which gold the view contents, except the nav bar objects. Can I override the pop and push methods to just animate this subview on and off screen?
Just do a push as normal but set it to not animate.
i.e.
[self.navigationController pushViewController:newViewController animated:NO];
Then when you want to go back...
[self.navigationController popViewControllerAnimated:NO];
Then you can have two navigation bars but it will look like it's the same bar as it isn't animating.
EDIT TO ALSO ANIMATE IN THE VIEW AND KEEP THE NAV BAR
I'm not sure of the whole flow of the app but if you want to keep the nav bar and swipe the new UI in then you could create a scroll view (with paging) and put the views of each VC on different frames of the scroll view or something.
Why do you want to keep the nav bar still anyway? There is nothing wrong with animating the nav bar and keeping the same buttons etc on it.
Having said that, if you are using different VCs then the nav bar should change anyway to show the details (i.e. title) of the VC you are currently looking at.
ANOTHER MORE RADICAL APPROACH
OK, thinking laterally now :D
How about, you use the not animated push and pop (as above) but instead of just displaying the UI you can animate it in from the relevant side. (A singleton or a VC subclass which you then subclass for your UI could do this for you across all view controllers).
The next problem is that it will look like the UI has gone instantly blank before animating in the new UI so you need to animate out the old UI. This means both UIs (the old and the new) have to be on the screen at the same time.
You can get round this by converting the entire view of the old UI into an image (not hard to do will find a link) and then passing this image into the new VC. The new VC will then instantly display this image and animate it out of the screen at the same time as animating its own UI onto the screen.
Really not as hard to do as it sounds. Especially if you subclass UIViewController ad give it a function animateUI and a property oldUIImage and direction. Then you can override viewWillAppear in this class to do the animation for you. Then the only thing you have to do is give each VC an image and a direction when you push/pop to it.
This is just giving the illusion of what you're after and means you can still keep a fairly simple object model and flow of the app.
...or something.
Just a riff on #Fogmeister's good idea...
In the presenting view controller, get self.view's image by implementing the suggestion here. Then, when it's time to present...
UIImage *image = [self makeImage]; // what it was called in the other post, consider a better name
MyViewController *newVC = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
newVC.presentationImage = image;
[self.navigationController pushViewController:newVC animated:NO];
In MyViewController, give it a UIImage property called presentationImage, then ...
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIImageView *imageView = [[UIImageView alloc] initWithImage:self.presentationImage];
imageView.frame = self.view.bounds;
imageView.tag = 128;
[self.view addSubview:imageView];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:128];
[UIView animateWithDuration:0.5 animations:^{
imageView.frame = CGRectOffset(imageView.frame, -self.frame.size.width, 0);
}];
}
FYI - I didn't test or even compile this. Just liked the idea and decided to stretch my fingers.
To do this in a "clean" way, you'd need to abandon UINavigationController and write a custom container controller that does not push new navigation items onto it's navigation bar when pushing a new view controller (or allows you to push the navigation item in a non-animated fashion while animating the push of the view controllers views).
However doing this will take some time. If you decide to do this, i recommend the WWDC Session on UIViewController containment.
Another alternative that springs to my mind is to (by subclassing or method swizzling) alter the behaviour of UINavigationController to push the navigation items non-animated while animating the viewcontroller-push.
I have in no way tested this, but overriding push (and pop respectively) in a subclass like this might work:
- (void)pushViewController:(UIViewController *)vc animated:(BOOL)animated {
[super pushViewController:vc animated:animated];
[self.navigationBar setItems:self.navigationBar.items animated:NO];
}
If this method doesn't work, it might be worth inspection what animations are going on inside the nav bar directly after the call to pushViewController:animated:. Maybe you can cancel some animations to go to the final state directly.

Present a UIView over a view

How can I make a custom view in iOS which appears above the existing view,but smaller? It should be like UIAlertView, but taken from a .xib file. After user taps a certain button, the small view vanishes and the normal view appears.
If this is posiible, how can I do it? And.. if it's not hard for you, please, include code.
Thanks in advance!
I think what you're looking for is a modal view. Modal views make it easy to have a view take over the screen for a little while, then when they get dismissed have the background view resume where it left off without having to worry about who's on top or handling events in partially-obscured views.
Here is Apple's article describing it.
They key is to have the controller class for your view call [self presentModalViewController:popupController animated:YES]; where "popupController" is the view controller for the view you want to show up. When you're ready to make it go away, call [self dismissModalViewControllerAnimated: YES];
You can just use the addSubview: method on your UIWindow or your visible UIViewController's view to show the UIView, and you can hide/remove it later by calling removeFromSuperview on the presented UIView.

How to know when StatusBar size changed (iPhone)

I have UITabBarController with 2 tabs. One resizes just fine, when StatusBar size changes (emulator "Toggle In-Call Status Bar" menu item). The other one doesn't.
The problematic tab item contains a static view, which dynamically loads one or another view depending on certain things. While getting this setup working I discovered that main tab view did NOT automagically send e.g. viewWillAppear and viewWillDisappear messages to my dynamic subviews.
Apple docs explained this was because dynamically added views were not recognized by the system.
#interface MyTabViewController : UIViewController
{
UIView *mainView;
FirstViewController *aController;
SecondViewController *bController;
}
...
if (index == 0)
{
self.aController = [[FirstViewController alloc]
initWithNibName:#"FirstViewController" bundle:nil];
[self.mainView addSubview:aController.view];
[self.aController viewWillAppear:YES];
}
How can I get StatusBar size changed event into my dynamic subviews? The "didChangeStatusBarFrame" doesn't work, as documented elsewhere.
Have you taken a look at this question?
Also, can we see your App Delegate code using application:didChangeStatusBarFrame:?
You could program a resize yourself, but usually this is done by using "auto resizing masks". (UIView has a property autoresizingMask).
I'm not sure about it but since your were adding those views programmatically maybe you forgot to set the autoresize mask. Without it the view won't resize automatically when the status bar frame changes.
[newView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth];
I hope it helps.

Displaying UIImagePickerController within another UIView

I've been working pretty extensively the last couple months with UIImagePickerController, particularly with the new capabilities in OS3.1 and newer to overlay views on-top of the camera view. This has worked just fine.
However, I am currently working on a project where I'd like to be able to display the camera view of the UIImagePickerController within an existing view. Essentially, the exact opposite of what I've currently been doing.
An example would be a View Controller with navigation components (Think top and bottom horizontal bars with gradients), and upon tapping a button on one of these bars, then content area displays the camera view. The shutter animation would should up, and the top and bottom navigation bars would remain always on-top.
I've had success adding the UIImagePickerController to the window view, as well as presenting it modally, but haven't had any luck adding it as a subView.
ex:
[window addSubview:camera.view];
[self presentModalViewController:camera animated:YES];
All you need to do is call viewWillAppear and viewDidAppear.
Here is an example where _pickerController is an instance of UIImagePickerController:
[self.view addSubview:_pickerController.view];
[_pickerController viewWillAppear:YES];
[_pickerController viewDidAppear:YES];
Call viewWillAppear:YES on the image picker controller after adding its view to your view. Skip the modal view controller business.
I don't think the API provides direct access to the actual view of the UIImagePickerController. The class is actually a subclass of UINavigationController so I don't think it has an actual view itself but rather manages the calling of its subcontrollers and their views.
When you call the UIImagePickerController modally, its doesn't add the views it controls as subviews to the window (or any other view). That is what a modal view means. It presents the view off to the "side" of the view hierarchy.
Even if you could hack this together, I think Apple would reject it as not being part of the API and for violating the HIG.