iPhone UINavigationBar - iphone

I'm using UINavigationController and I have a UIView added to self.window
UIView *myView = [[UIView alloc] init];
myView.frame=CGRectMake(0,20,320,100);//20 is to position view under status bar
[[[self.window subviews] objectAtIndex:0] addSubview:myView];
This is done in AppDelegate.m file.
And this way myView covers up the navigation bar completely.
However, when the screen orientation changes, somehow the navigation bar is brought to the very front (just like what bringSubViewToFront does) and is on top of myView (a portion of view is covered up by navigation bar).
Does anyone know why this happened? Is there a way I can keep myView on top of everything?

If you don't want to show the navigation bar, just hide it:
self.navigationController.navigationBarHidden = YES;
or:
[self.navigationController setNavigationBarHidden:YES animated:YES];
if you want to animate the change from not hidden to hidden.

You can also adjust the "z" coordinate for stuff on the screen
[aview sendSubviewToBack:asubview];
and
[aview sendSubviewToFront:asubview];

Related

IOS: How to put some view on top of presented modal view controller?

I have an activity view that I have added in AppDelegate class to tap bar:
[self.mainTabBar.view addSubview: spinner];
When there are connection problems it is visible in each view controller and is spinning.
There is some button at certain view controller, makes to present some modal view controller.
That modal view controller overlaps the spinner. How to make that spinner always be on top of all views or at least on top of that modal view controller?
I tried to make such a thing in view controller that presents modal view controller:
[self presentModalViewController:selectionViewController animated:YES];
[self.view bringSubviewToFront:[self.tabBarController.view viewWithTag:15]];
Not works.
Add the view to the main window.
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview: spinner];
While phix23's answer is correct, here is a more complete example:
//The view you want to present
UIViewController *viewControllerYouWantToPresentOnTop = [[UIViewController alloc] initWithNibName:nil bundle:nil];
//Create transparent host view for presenting the above view
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
UIViewController *viewControllerForPresentation = [[UIViewController alloc] init];
[[viewControllerForPresentation view] setBackgroundColor:[UIColor clearColor]];
[[viewControllerForPresentation view] setOpaque:FALSE];
[mainWindow addSubview:[viewControllerForPresentation view]];
//Make your transparent view controller present your actual view controller
[viewControllerForPresentation presentViewController:viewControllerYouWantToPresentOnTop animated:TRUE];
Remember to clean up after yourself when you don't need these any longer.
This code can be used from anywhere in your app, even a library :)
An app normally displays its content within a single window throughout its life.
But there are situations where an extra window may be used to add content on top of everything else. Apple ensures UIAlertView always stays on top by adding it in a separate window.
UIView *contentView = [[UIView alloc] initWithFrame:contentFrame];
contentView.backgroundColor = [UIColor greenColor];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(x,y,contentFrame.size.width, contentFrame.size.height)];
window.windowLevel = UIWindowLevelAlert;
[window addSubview:contentView];
[window makeKeyAndVisible];
Show and hide your window by setting window.hidden = Yes or No as needed.
This will always show your contentView on top of everything else in the app.
The modal controller is in a completely different layer, you cannot make any subview of the presenting controller to overlap it.
Use a UIAlertView with a spinner inside. The alerts are displayed in a layer which overlaps even modal controllers.
Place the view to the keyWindow, as suggested above. You might also need to set Presentation style of the modal view as Current Context, otherwise, it can still pop on top

addSubView to viewController.navigatorController

I have a viewController and I am trying to add a subview to it such that it will cover the whole screen, however this has a navigationController in it so that adding a subView always adds it below the navigation bar, is there a way to simulate a presentModalViewController in cases like this?
You can add the subview to the view controller and then hide the navigation controller from the top or you could still push it to the navigation controller and then just remove the navigation controller from the top again and then you could use pop to go back and forth.
the code to push a view controller is
if(!self.YOURVIEWCONTROLLER){
self.YOURVIEWCONTROLLER = [[YOURVIEWCONTROLLER alloc] initWithNibName:#"YOURVIEWCONTROLLER" bundle:nil] autorelease];
}
[self.navigationController pushViewController:self.YOURVIEWCONTROLLER animatedLYES];
and on the next NEXTView.m add
[self.navigationController setNavigationBarHidden:YES];
remember to create an instance of YOURVIEWCONTROLLER in the .h file. Or you could do a simple
[self.view addSubview:NEWVIEW];
[self.navigationController setNavigationBarHidden:YES];
at least at bear minimum the line for making the navigationController hide is there.
I seem to recall once having a similar problem, and I seem to recall the solution was to add the subview to the navigation controller (as the view controller is already a sub view of the navigation controller) rather than adding it to the view controller.
I recently have similar problems and after spending 5 to 10 min I get the exact solution...
According to my solution I simply add my custom UIView to subview of navigationController.view
Like This :-
[self.navigationController.view addSubview:popOver];
popOver - Your custom UIView
Happy Codding :)
Add the view to the superview of the navigationController's view.
[navigationController.view.superview addSubview:viewController.view];
Perhaps you could hide the navigation bar when you add the subview. I have a method on my ViewController that looks like this:
self.navigationController.navigationBarHidden = YES;
UIView *v = [[UIView alloc] initWithFrame:self.view.frame];
v.backgroundColor = [UIColor redColor];
[self.view addSubview:v];
When that code executes, my navigation bar disappears and a full screen red view replaces it.

Remove a toolbar when pushing a new view

In the iPhone maps app there's a toolbar at the bottom of the map view (it contains the Search/Directions segment control and others). When moving from the map view by clicking on a callout, the toolbar slides out with the map view, leaving the next view (a table controller) with no toolbar.
I've tried to do the same thing with [self.navigationController setToolbarHidden:YES animated:YES] in the second view controller, but this gives a strange toolbar sliding down animation, while the map view is sliding to the left.
Using [self.navigationController setToolbarHidden:YES] in viewDidLoad also causes a bad effect (it makes the toolbar disappear the moment the push animation starts, leaving an ugly white space).
I'm assuming the answer to this is to use a nib file, but I'd prefer to do it programatically (if possible).
How can I get the toolbar to "stick" to the map view and slide out with it when I push a new view controller? Thanks.
Gourmet Haus Staudt http://img.skitch.com/20100518-xfubyriig48d3ckaemjg2ay8q.jpg
It turns out the answer is to create the toolbar directly and add it to the view yourself. This is in the code for a UIViewController with a UINavigationController. The frame coordinates can change according to what is on screen.
- (void)viewDidLoad
{
// Add a toolbar to the view
CGRect toolbarFrame = CGRectMake(0, 372, 320, 44);
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
UIBarButtonItem *compassButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"compass.png"]
style:UIBarButtonItemStyleBordered
target:self
action:#selector(zoomToCurrentLocation)];
compassButton.width = 30.0f; // make the button a square shape
[myToolbar setItems:[NSArray arrayWithObject:compassButton] animated:NO];
[compassButton release];
[self.view addSubview:myToolbar];
[super viewDidLoad];
}
I was around this for a day once. Really dont get the programatically answer, but the best way to views to behave correctly, is to do the interface in the interface builder. If you set items for a toolbar in your code like:
[self.navigationController setToolbarItems: control1, control2,..., nil] animated: NO];
with my little experience, I can say that you are saying to the entire application to have a toolbar present when you push new views unless you hide it (or you are using a tabBar), but hiding it you get those unwanted effects.
You can try this:
[self.navigationController setToolbarHidden:YES animated:YES];
in your first controller - (void)viewWillDisappear:(BOOL)animated method,
and setting hidden to NO in - (void)viewWillAppear:(BOOL)animated method in the first controller too.
Hope this helps.
PS: And if you get the programatically answer, let me know! =P
Override the second view controller's -viewWillAppear: method to hide the toolbar.

UIView disappears after modalViewController is removed from superview

In my app I have a tabBarController and in it a navigationController. One of my view controllers is a TableViewController and under the navigationBar i added a uiView as a subview to the view like this:
rectangleInfo = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] applicationFrame].size.width,26)]; rectangleInfo.autoresizingMask = (UIViewAutoresizingFlexibleWidth); rectangleInfo.backgroundColor = [UIColor darkGrayColor]; [self.view addSubview: rectangleInfo];
when I click on a cell in the tableView I push an UIViewController like this:
[feedViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[[self navigationController] presentModalViewController:feedViewController animated:YES];
After i pop the modal view for a couple of times with it from the tableViewNavigationController disappears the rectangleInfo UIView.
I pop my modalview like this:
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
[self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self dismissModalViewControllerAnimated:YES];
any idea why that subview (rectangleInfo) of the tableViewController dissapears after i remove the modal view from the superview?
thank you in advance.
I'm curious as to what you are trying to do with your rectangleInfo view? By the size of it, it looks like you are trying to mimic the status bar. Why? You can just hide the status bar if you want.
Another thing you can try is to create the UIView visually in Interface Builder. Don't actually add it to your main view, but create it as a separate UIView in the XIB with the appropriate size, etc. Then create an outlet for it in Xcode and connect it. Next, add it as a subview in code when your view controller loads. See if that makes a difference. This is especially strange since you say it only disappears after popping it several times. Do you get the same problem if you push and pop the non-modal way, e.g.:
[[self navigationController] pushViewController:feedViewController animated:YES];
[[self navigationController] popViewControllerAnimated:YES];
i solved the problem by implementing correctly the viewDidLoad and viewDidUnload methods for creating and releasing my subviews.

UITableView scrolling under the navigation bar

I have a table view setup which currently, when being flickered up, has its sections flush up against right underneath the status bar, instead of flushing against the the navigation bar. I'm not sure if this is the proper behavior, but most applications have the Section Title flush properly below the navigation bar when it's slid into view.
What's the right way to correct this instead of downsizing the tableView arbitrarily?
* EDIT *
Related to a thread I created in Broken cell with an odd strikethrough?. This problem plus the 'cell strike-through' problem occurs when I set my Navigation Bar to a Translucent Black. When it's Black Opaque or Normal, such a problem does not exist. I'm not sure if that's a result of something else in my code or an issue with the SDK.
Sounds like you either don't have the bounds set properly in IB, or your springs-and-struts aren't correct. Is this the top level of the UIViewController, or a subview? Are you using a UINavigationController? If you test the interface in IB, does it look okay?
I had a similar problem and it gets fixed automatically by setting this
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
As I indicated in the other post, I suspect the hierarchy of views under the UINavigationController has become disrupted.
The layoutSubviews in the content view (which contains the navigation bar and your UITableView) of the UINavigationController should be sizing the UITableView such that it doesn't overlap the navigation bar. In your case, I'm guessing that either the UITableView is resizing itself afterwards or the layoutSubviews doesn't know about the UITableView for some reason and the UITableView is passing underneath the navigation bar, causing the alignment issue you are seeing.
I had the same problem on iOS 5.1 using the following code:
Create the navigation controller & add a table view
UINavigationController *navigationController = [[UINavigationController alloc] init];
[navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
[navigationController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[navigationController.navigationBar setBarStyle:UIBarStyleBlack];
[navigationController.navigationBar setTranslucent:TRUE];
[navigationController setNavigationBarHidden:NO animated:NO];
[self presentModalViewController:navigationController animated:YES];
MyTableViewController *aTableViewController = [[[MyTableViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
aTableViewController.navigationItem.rightBarButtonItem = buttonItem;
[navigationController pushViewController:aboutTableViewController animated:YES];
Add a Table Header View to the table
ATableHeaderView aTableHeaderView = [[[ATableHeaderView alloc] initWithFrame:aboutTableView.frame] autorelease];
[aTableHeaderView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin];
[aTableHeaderView sizeToFit];
[aTableView setTableHeaderView:aTableHeaderView];
Inside the table header view I added some labels
UILabel *aLabel = [[[UILabel alloc] initWithFrame:CGRectMake(x,y, width, height)] autorelease];
[aLabel setText:aString];
[aLabel setAutoresizesSubviews:YES];
[aLabel setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
I ended up with the table header content under the navigation bar. Changing the navigation bar to solid black fixed it. But this is not what I wanted. After some trial and error I removed the line:
[aLabel setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
From the setup of the UILabel in the header and the problem is fixed. I have a translucent header and the content of the table is positioned properly.