I have the starter point of my application this window (white background):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
mainScreen = [[MainScreenController alloc] initWithNibName:#"MainScreenController" bundle:[NSBundle mainBundle]];
// add it to the main window
[window addSubview:[mainScreen view]];
// show view
[window makeKeyAndVisible];
NSLog(#"started");
return YES;
}
And MainScreenController is an UIViewController with (blackBackground) created by the Interface Builder.
The application executes perfectly but the first time, the black view is moved up like the height of the status iphone pannel (leaving white rectangle at the bottom).
If I rotate the iPhone twice, the view is perfectly positioned in the right place, filling all the screen with the black background.
Any idea?
Thanks!
Check your MainScreenController settings in IB. You should select a simulated status bar element (cmd-1) so that the view is positioned below the status bar initially.
EDIT - Why are you calling the initWithNibName in your delegate here? Most of the app templates I've used had the main view nib initialized by the OS and you use IB and set an IBOutlet in the main VC interface. You probably have an initial frame origin of {0,0} after calling initWithNibName. Add these lines after your initWithNibName call to verify this:
NSLog(#"mainscreen frame=%#", NSStringFromCGRect(mainscreen.view.frame));
CGRect frame = mainscreen.view.frame;
frame.origin.y = 20; // move below status bar
mainscreen.view.frame = frame;
Related
iPad app. I have a tabbar. One tab has a view controller with a view. In that view controller I add a uinavigationcontroller. If I start the app in portrait, everything works fine. However if the app starts in landscape, my navbar y position is 20 (instead of 0 which it should be). I can see the view behind the navbar. If I manually re-position (a bandaid) the uinavigationcontroller to -20 y, I get a host of sizing problems throughout the app. How can I fix this? (by the way, if i remove the status bar it sizes just fine in landscape)
Here is the code of my view that manages the navcontroller:
- (void)viewDidLoad
{
[super viewDidLoad];
self.topicsList = [[TopicsListVC alloc]initWithStyle:UITableViewStylePlain];
self.navController = [[UINavigationController alloc]initWithRootViewController:self.topicsList];
self.navController.view.frame = self.view.frame;
[self.view addSubview:self.navController.view];
i'm working on a uitabbar application and im wondering if its possible to put a view inside it.
my main purpose for this is i 2ill put a semi-transparent music player with about 20 pixels above the tabbar so when the user switches between other views the music play wont stop.
You can see a similar solution in Madonna App (whis is made with Mobile Roadie).
Thanks for helping again. hope someones knows this
You can put views "on top of" your entire application by putting them as subviews of the app's window. For example, this will "float" a red rectangle over your entire app (I tried it out in a tab-bar based app and it worked great):
- (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];
UIView *myView = [[[UIView alloc] initWithFrame:CGRectMake( 50, 50, 50, 50 )] autorelease];
myView.backgroundColor = [UIColor redColor];
[self.window addSubview:myView];
return YES;
}
Hi i have a strange issue after adding my UIViewController.view to my Application Window.
I created a window based app and added my view in my appDelegates didFinishLaunchingWithOptions method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
overviewViewController *overView = [[overviewViewController alloc] initWithNibName:#"overviewViewController" bundle:nil];
//overView.view.bounds = CGRectMake(0.0f, 20.0f, 320.0f, 460.0f);
[window addSubview:overView.view];
//[overView release];
[window makeKeyAndVisible];
return YES;
}
clicking the "add" button presents a model view controller. after dismissing this modelViewController the main view fits perfectly.
as you can see, i also tried to set the bounds before adding my subview, without any success.
does anybody have some hints for me please, how to solve this problem?
You can fix this by setting this in your viewDidLoad method or even the viewWillAppear:
self.view.frame = [[UIScreen mainScreen] applicationFrame];
Also, I'm guessing you are hiding your status bar initially? Or do you have it always showing?
in your nib, check to make sure the simulated interface status bar is turned on so that it shows up when your editing your nib's view.
if this does not work, try setting the frame instead of bounds before you add it to the window.
My iphone application is showing strange behavior when rotating: a gap appears between the navigation title and content view inside a tab bar view (details on how to reproduce are below). I've created a tiny test case that exhibits the same problem: a custom root UIViewController, which creates and displays a UITabBarController programmatically, which has two tabs: 1) plain UIViewController, and 2) UINavigationController created programmatically with a single plain UIViewController content view.
The complete code for the application is in the root controller's viewDidLoad (every "*VC" class is a totally vanilla UIViewController subclass with XIB for user interface from XCode, with only the view background color changed to clearly identify each view, nothing else).
Here's the viewDidLoad code, and the shouldAutorotateToInterfaceOrientation code, this code is the entire application basically:
- (void)viewDidLoad {
[super viewDidLoad];
FirstVC *fvc = [[FirstVC alloc] initWithNibName:#"FirstVC" bundle:nil];
NavContentsVC *ncvc = [[NavContentsVC alloc] initWithNibName:#"NavContentsVC" bundle:nil];
UINavigationController *svc = [[UINavigationController alloc] initWithRootViewController:ncvc];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
[localControllersArray addObject:fvc];
[localControllersArray addObject:svc];
fvc.title = #"FirstVC-Title";
ncvc.title = #"NavContents-Title";
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.view.frame = CGRectMake(0, 0, 320, 460);
[tbc setViewControllers:localControllersArray];
[self.view addSubview:tbc.view];
[localControllersArray release];
[ncvc release];
[svc release];
[fvc release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
Here's how to reproduce the problem:
1) start application
2) rotate device (happens in simulator, too) to landscape (UITabBar properly rotates)
3) click on tab 2
4) rotate device to portrait -- notice gap of root view controller's background color of about 10 pixels high beneath the Navigation title bar and the Navigation content view.
5) click tab 1
6) click tab 2
And the gap is gone! From my real application, I see that the gap remains during all VC push and pops while the NavigationController tab is active. Switching away to a different tab and back to the Nav tab clears up the gap.
What am I doing wrong? I'm running on SDK 3.1.3, this happens both on the simulator and on the device. Except for this particular sequence, everything seems to work fine. Help!
This problem occurs when you nest a UINavigationController within another UIViewController (in this case a UITabBarController). If you had the UINavigationController as the root view controller, then this problem wouldn't occur.
One solution may be to go in and alter the frame of the navigation bar (set the y origin from 0 to 20), but the documentation states explicitly not to do this. So to me, this is an indication that it isn't considered good UI to nest a UINavigationController - you shouldn't be doing it.
Please let me know what you think - thanks. :)
A workaround works in some occasion:
After rotating, force a refresh of the NavigationBar and therefore the frame of its view is resized properly. Some code like this:
(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// if _navigationController is showing
[_navigationController setNavigationBarHidden:YES];
[_navigationController setNavigationBarHidden:NO];
}
I was wondering how I would go about creating a view that persists through all the over views and controllers in the application. Similarly to how Soundcloud does it with their music player in their iPhone app (See pic). No matter where you go in the application the music player view stays at the top below the toolbar.
alt text http://i1.soundcloud.com/screenshots-000000000025-akaqgr-t500x500.jpg
The basic answer is to create a view at the top of the view hierarchy.
Let's take the example of your tab view-based screenshot above. Let's say the root view controller for the app is a subclass of UITabBarController called RootViewController.
Normally your application delegate will have a method that will look similar to this:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[window addSubview:rootViewController.view];
[window makeKeyAndVisible];
}
If you want your view to just "overlap", that's simple. Let's say you want your "persistent view" to be 100 pixels tall and stretch the width of the screen. Assume you've already created a class for this view, and it's called PeristentView. Your code would look something like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[window addSubview:rootViewController.view];
PersistentView* persistentView = [[PersistentView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[window addSubview:persistentView];
[persistentView release];
[window makeKeyAndVisible];
}
The order here is important because you want the persistent view to be below in the view hierarchy, so that the rootViewController's view draws first, then the persistent view draws second.
If you want the persistent view to "push down" the the rest of the views on the screen, you'll have to change this up a bit. Given that by default you are telling the window to add the entirety of the root view controller's view as a subview, what you want to do instead is resize the frame of rootViewController.view so it doesn't take up the whole screen and reposition its origin.
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
PersistentView* persistentView = [[PersistentView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[window addSubview:persistentView];
[persistentView release];
CGRect frame = rootViewController.view.frame;
frame.size.height -= persistentView.frame.size.height;
frame.origin.y += persistentView.frame.size.height;
rootViewController.view.frame = frame;
[window addSubview:rootViewController.view];
[window makeKeyAndVisible];
}
I just tested this with an existing tab bar-based project (using a plain UIView instead of creating a PersistentView class to bootstrap the test) and it works fine.
You can add a view as a subview of your main window, and position it above your tabBarController's view. It will then always be visible.