Persistent UIBarButtonItem in UIToolbar? - iphone

I've been developing an iPhone app that uses a UIToolbar (in the context of a UINavigationController) to display a small status icon at the bottom of the screen. The toolbar will eventually also have action icons associated with it (think Mail application).
I'm running into an issue where it appears that each UIViewController pushed onto the navigation controller's stack is expected to have its own set of items for the toolbar to display, and as a result, the "status" item fades out and back in for each view transition.
Is there a way to have a single persistent item in the toolbar? I also tried adding the item in the navigation controller's initializer (I subclassed UINavigationController for this approach), but it's still no go.

Instead of using the navigation controller's toolbar, add one directly to the window and resize the navigation controller's view's frame to avoid it. That single global toolbar will then always be visible.
If you're using the Navigation-based Application template, and are using Interface Builder, the following steps should do it:
Open up your app delegate's .h file.
Add an IBOutlet UIToolbar * toolbar; to the app delegate's instance variables.
Switch to the .m file.
Find the line that reads [window addSubview:[navigationController view]]; and add after it:CGRect frame = navigationController.view.frame;frame.size.height -= toolbar.frame.size.height;navigationController.view.frame = frame;
Add code to release toolbar in the -dealloc method.
Open MainWindow.xib.
Open the window.
Drag a toolbar onto the bottom of the window.
Connect the toolbar to the app delegate's toolbar outlet
Now set up the toolbar—add whatever items you need to it, then create whatever outlets and actions you need in the app delegate and connect them.
Since the toolbar is part of the window, not part of the navigation controller, the navigation controller shouldn't touch it.

As you push new views onto your navigation controller the views in the toolbar will be replaced with the views from the toolbar of the view on the top of the stack.
Even if you have a static view (a view that does not change when you push a new controller), the view will still appear to be new because of the animation apple includes when you push a new view controller onto the stack.

I found this works for me:
CGFloat height = [self.toolbar frame].size.height;
CGRect rootBounds = self.window.rootViewController.view.bounds;
CGRect frame = CGRectMake(0, CGRectGetHeight(rootBounds) - height, CGRectGetWidth(rootBounds), height);
[self.toolbar setFrame:frame];
[self.navigationController.view addSubview:self.toolbar];

Related

Display whole ViewController within another ViewController's view

Im writing an application which the main view controller is a UIViewController. It has some icons in a grid and I want to dismiss (sliding down) this grid when one of the icons is clicked. This I've done already. The problem is: when the grid is dismisseed I want another View to come from the top of the screen. This view is in this same root view controller. But I want to display the content of other view controllers in this view. For example: I want this view to show a UINavigationController with a UITableView inside it, so the user can navigate through TableViews.
I'm doing this:
HorariosViewController *horarios = [[HorariosViewController alloc] init];
[vuashView addSubview:horarios.view];
HorariosViewController is a UINavigationViewController. It shows me only a blue NavigationBar and changes like self.navigationItem.title = #"Title" won't work.
Thanks!
You can show another view controller's views as subviews but their outlets and actions remain linked to their original view controller unless you write code to make new connections, so self.whatever shouldn't be expected to affect the other view controller's properties.
(Also, if HorariosViewController is a UINavigationController, it shouldn't be created as a UIViewController.)
One approach is to have the navigation controller already there, with the icon grid presented modally on top of it. (you can set the view up this way without animations, so the user doesn't see the navigation controller underneath).
Then, when it's time for the grid to go away, it can call dismissModalViewController on itself with animation.

hide/show Scrollview in Navigation Controller

I am using a Navigation Controller. On top of the AppDelegate window I added a view that contains a scrollview and toolbar that I want to be using throughout the app. They appear fine on all the view controllers that I am using in the navigation controller. Now I would like to be able to hide and show them from each view controller.
I can't figure out how that should work,
any suggestions?
I am not sure if I understand you correctly but if you just want to hide/show scrollbars in each viewcontroller just call:
myScrollView.showsVerticalScrollIndicator = NO;
myScrollView.showsVerticalScrollIndicator = NO;
in init, viewDidLoad or any custom method in your UIViewController subclasses.

How to reliably place built-in UINavigationController UIToolbar on top of iPad view

I am trying to use the UINavigationController object's built-in UIToolbar object in my iPad application, but I want it to be displayed on top of the view instead of the bottom, which is where it defaults.
I am also hiding the UINavigationController object's Navigation Bar.
In order to make this work, I had to write the following code:
navigationController.navigationBarHidden = YES;
navigationController.toolbarHidden = NO;
navigationController.toolbar.frame = CGRectMake(0, 0, 768, 44);
This solution works with one exception: when the application Enters Background and Becomes Active again, the Toolbar is always repositioned on the bottom of the view.
I've tried moving the code from viewDidLoad to viewDidAppear:animated, and it still behaves this way.
First, is there any better way to approach this, and if not, how can I stop the Toolbar from being repositioned?
I've also instead decided to use my own UIToolbar object and add it to each view via a custom Base UIViewController class' viewDidLoad. However, this causes the Toolbar to animate when each view is pushed or popped because it is actually part of the view, which just seems "hokey".
Any ideas on possible solutions?
Thanks everyone!
It says in the documentation under UINavigationController's toolbar property that:
Access to this toolbar is provided solely for clients that want to present an action sheet from the toolbar. You should not modify the UIToolbar object directly.
This is sort of hackish, but you could make a UIToolbar yourself and add it directly to the window (i.e. over the navigation controller.)
You can use the category below to modify the UIToolbar class to achieve what you're after.
#implementation UIToolbar (setCenter)
-(void)setCenter:(CGPoint)center
[super setCenter:CGPointMake(384, 22)];
}
#end
The toolbar has limited functionality when used with a UINavigationController. It only provides a convenient way to manage the actionsheet in the toolbar.
From the docs: "Access to this toolbar is provided solely for clients that want to present an action sheet from the toolbar. You should not modify the UIToolbar object directly."
The solution I would use is to create a subclass of UIView with convenience methods to manage your actionsheet and any other custom functionality you need. This custom view can be shared across all views in the UINavigationController and placed where ever you like in the parent view. This will give you ultimate control of your custom top placed toolbar.

Navigation Bar with prompt in UIpopover

I have a navigation Controller as the view of a popover, so that there is a navigation bar at the top.
On the first view there is no prompt for the navigation bar, so it remains at it's usual small size.
I then push the next view controller which does need a prompt and the bar expands, except behind the view, hiding the Title and Back button.
If I comment out the code in loadView, so that self.view is never set, then you can see the backbutton and title, but you can't click on the back button, as if it was behind another view.
I never had this problem in 3.2, only now in 4.2
Here you can set the size of popover using the following code:
self.contentSizeForViewInPopover = CGSizeMake(320, 460);
You need to set the content size of popover in view using this code and you can add this code in the viewdidLoad mehtof of the controller. Let me know if you still have any question.

Full screen UIImage view

I have an application with a navigation bar and a tab bar. A user can navigate to a view which displays images in a scroll view. I'd like to have the same behavior as the iPhone photo app: Nav bar at the top, tool bar at the bottom, which will hide or show based upon a tap.
I'm moving my view to the window object in order to achieve full screen mode. This works fine:
myView = [self.view retain];
self.view = nil;
[window addSubview:myView];
But when I want to redisplay the Nav & tool bar, I run into a problem. The bars show fine, but the view is empty, and I can't seem to add any content to the view:
[myView removeFromSuperview];
self.view = myView;
I got a lot of good info from this post
but can't quite get the right combination.
By simply setting the controller's view, you aren't adding it as a subview to anything else, so it will never appear.
Moving views around like this can get a little tricky. I recommend that you not move the view from one to the other, but instead have two UIViews. Add second UIView to the window's subview and set it to hidden=YES initially. When you want to show it, set the image for the UIImageView, and then set the hidden property to NO.
what's wrong with just using setNavigationBarHidden: animated: and setToolbarHidden:animated:?