I have a Button on a UIViewController. When i click on this Button, a UIToolBar gets displayed (Above the TabBarController). Now when i scroll the view down and click the Button the UIToolBar gets displayed at a different location (way above the tababar). What i want is to display the UIToolBar right above the TabBar at all instances (even when i scroll the page up or down and click the Button).
My code so far :
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 300 , 320 , 55)];
According to this Code, i have given the X and Y cordinates of the toolBar to be 0 and 323. So even when i scroll the view up or down and click on the button, the toolbar will display at this location.
So how can i modify my code, to display it right above the TabBar at all instances. (Even when i scroll the view up or down)
It all depends where you will add your toolBar as a subview. If you want it always to be in the same place add it to the current UIWindow object.
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 300 , 320 , 55)];
on button click add the toolBar as a subview to the main window
[mainWindow addSubview:toolBar];
the problem is: You are adding the toolbar on the view with the coordinate hardcoded to (0, 300 , 320 , 55). And this is with respect to the view. But not the iphone screen or window. after adding when you scroll the view, you can see that the view contents are also scrolled up.
All you have to do is:
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 300 , 320 , 55)];
[mainWindow addSubview:toolBar];
[mainwindow bringSubviewToFront:toolBar];
It sounds like you need to implement the following hierarchy:
UITabBarController
V
UINavigationController
V
Your view controller
A UINavigationController has a toolbar property that can be displayed or hidden using its setToolbarHidden:animated: method.
The toolbar appears directly above the tab bar, as per your question.
If you add a toolbar as a subview of your scroll view, you need handle the scroll view delegate method scrollViewDidScroll: and reposition the scroll view each time it's fired.
That method - and adding a scroll view to the main window - both have the downside of covering part of the scroll view. Using a nav controller's toolbar, the height of your view controller's view is automatically reduced by the height of the toolbar so there is no overlap.
See the Apple documentation for a full description.
Related
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];
I want to display a UIViewController as a small popover over an other UIViewController.
The UIViewController should be display modal, but not take the whole screen.
It should just show up on a lower third of the screen...
Can somebody point me to a tutorial or give me some hints to start on this?
I googled for over one hour, but could not find anything, that helps me :(
Thanks for your help,
Stefan
Just discovered that this question is still unanswered ... You have multiple options to achieve such a thing running on iPhone:
Depending on the situation, I think I would just go with a UIViewController subclass loading from a NIB that you configured to show a screen-sized view with the backgroundColor property set to [UIColor clearColor]. Add whatever subviews you want to display as a popover to that view at the position you want (e.g. lower third of screen). You can then present the view controller modally with the UIModalTransitionStyleCrossDissolve to fade it in and even use the background view to intercept touch events to dismiss the modal view.
Another possibility would be to just add the popover's view controller's view as a subview to the main view controller's view. You can use UIView's animation class methods to animate the transition.
this might help.
you have to declare popOverController in the header and make it a property as well as synthesize it.
if([self.popOverController isPopoverVisible])
{
[self.popOverController dismissPopoverAnimated:YES];
return;
}
UINavigationController *favNav = [[UINavigationController alloc]
initWithRootViewController:favoritesView];
//favoritesView is an outlet to the VC Favorites
//make a nav controller with the root view an outlet to the view you want to present.
self.popOverController = [[[UIPopoverController alloc]
initWithContentViewController:favNav] autorelease];
[popOverController presentPopoverFromBarButtonItem:revealFavorites permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //revealFavorites is the button i press to show the favorites popover
favoritesView.view.frame = CGRectMake(10, 10, 310, 320); //set the frame
if (![self.popOverController isPopoverVisible]) {
[favNav release];
}
}
I have a tab bar application and when I display a modal view controller, the content screen is offset by about 20 pixels to the top and left. It looks like this:
I'm presenting this view from the child view controller (detail view) of navigation controller (main view) of the tabview.
When I show the view, I'm hiding the tab bar and navigation bar but status bar is kept visible. Adjusting the view to be centered (through Interface Builder's Simulated Interface Elements->View Mode : Center) and changing the view's frame after a call to 'viewDidLoad' in the controller doesn't seem to shift it.
- (void)viewDidLoad {
// this still doesn't cause it to shift
self.view.frame = CGRectMake(0, 20, 320, 460);
}
What's the way to adjust this so that the content area is shown correctly?
I launch the child view like this:
[detailController presentModalViewController:tvc animated:NO];
The app's view controller hierarchy:
Tab view with two child navigation controllers are created in the app delegate and the nav controllers added to the TabBar's view controllers:
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:tab1ViewController,
tab2ViewController, nil];
[window addSubview:tabBarController.view];
Each view controllers of the tab is created as a NavigationController with 1 view controller:
// MainViewController inherits from UIViewController
[MainViewController *viewController = [[MainViewController alloc] initWithNib..];
tab1ViewController.viewControllers = [NSArray arrayWithObject:viewController];
A detail view controller is launched with 'pushViewController' as a result of some action on tab1ViewController :
DetailController *detailController = [[DetailController alloc]
initWithNibName:#"DetailView"
bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:detailController animated:YES];
[detailController release];
It's from the detailController that I'm trying to launch the problem controller.
Some things to check right off: is "viewDidLoad" actually getting called?
If so, what is self.view.frame set to after the assignment?
Put an NSLog at the end that prints out the x, y, width, height, and see what's there.
Also, since the trouble vc is modal, it will occupy the entire screen.
"On iPhone and iPod touch devices, the view of modalViewController is always presented full screen."
HTH,
Mike
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.
I am using a UITabBar control from library in one of my view (note that I am not using UITabBarController but the UITabBar control).
Now, I am adding two tabBar items to this tabBar.
I have created controller class for this view (.m and .h) files and used delegates in the .h file.
In the .m file I have used the following function:
(void)tabBar:(UITabBar *)TabBarControl didSelectItem:(UITabBarItem *)FirstView
I have assigned tag = 0 and tag = 1 to respective tabBar items.
What I want to do is that, on click of first tabBar item I want to show a view and click of another tabBar item, I want to show another view.
So, in the above function I am checking that if the tag of clicked tabBar item is 0 than I will show one view else I will show another view.
I am showing the view as following:
Team1Scoreboard *tempTeam1Scoreboard = [Team1Scoreboard alloc];
tempTeam1Scoreboard = [tempTeam1Scoreboard initWithNibName:#"UserTeamScoreboard" bundle:[NSBundle mainBundle]];
self.cntrlTeam1Scoreboard = tempTeam1Scoreboard;
[tempTeam1Scoreboard release];
UIView *theWindow = [self.view superview];
[self.view removeFromSuperview];
[theWindow addSubview:self.cntrlTeam1Scoreboard.view];
Now the problem is that, when I click on any of the tabBar item, it will load the correct view but the tabBar itself will be disappeared as I am adding the view to window itself.
Please help me so that I can load correct view and also my tabBar itself is visible.
The TabBar is disappearing because it's a child of the view which you are then adding a new child to and the new child is sized the same as the parent. Did that make sense? Ok, look at it this way:
You have ViewA and ViewA has a couple of labels and a TabBar. ViewA is managed by ViewControllerA. In ViewControllerA you are creating an instance of ViewB and calling ViewControllerA.view addSubView:instanceOfViewB, right? Before doing that, you will want to resize ViewB.
Try something like this:
ViewControllerB *viewControllerB = [[ViewControllerB alloc]initWithNibName:#"ViewB" bundle:nil];
CGRect frame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height - 40);
viewControllerB.view.frame = frame;
[self.view addSubview:viewB.viewControllerB];
Basically it should be close to what you are doing, but I'm setting the size to be 40 px less (whatever you need to remove the tab bar).