Standalone UINavigationBar? - iphone

I am looking to create a bar with a couple of buttons at the top of the iPhone interface just under the statusBar, the controller is a simple UIViewController so this is a bar for buttons not for navigation. I have got things working but it feels a little clunky and I was wondering if I was doing it right and not missing any shortcuts.
There is no action / selector: set yet as I am just setting this up.
I will probably remove the NSArray convenience method and add an alloc/release.
Code:
- (void)loadView {
// VIEW
UIView *tempView = [[UIView alloc] initWithFrame:screenFrame];
// NAV BAR
UINavigationBar *tempNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[tempNavigationBar setBarStyle:UIBarStyleBlack];
UIBarButtonItem *testButton = [[UIBarButtonItem alloc] initWithTitle:#"TEST" style:UIBarButtonItemStyleBordered target:self action:nil];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
[navItem setLeftBarButtonItem:testButton];
[testButton release];
[tempNavigationBar setItems:[NSArray arrayWithObject:navItem]];
[tempView addSubview:tempNavigationBar];
[tempNavigationBar release];
[navItem release];
[self setView:tempView];
[tempView release];
}

That looks alright to me. An alternative would be to do this in Interface Builder, but that's a completely different matter.
If you intend to support autorotation, you may have to add some more code to set autoresize masks for your navigation bar so it expands correctly. Actually, I would use Interface Builder for this, because I'm big on visualizing my autoresize masks.

Yeah the question repeats but why you don't preferred I.B.? As you don't need navigational functionality then you need to look into an alternate i.e. UIToolBar comes to mind. its usage is same as navigation bar as you need to setItems with NSArray as you are doing.
I hope you will succeed in finding your proper solution.

Related

Infobutton not Displaying ModalView

When pressing infobutton it is not displaying ModalView
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(displayModalView:)];
- (void)displayModalView
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[Infoviewcontroller alloc] init];
UINavigationController *navigationController=[[UINavigationController alloc] init];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[navigationController pushViewController:_viewController animated:YES];
[_window addSubview:navigationController.view];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
}
Anyone can help me please what is the problem in this.
Thanks a lot in advance for helping me out
In your question you didn't specify how you created your objects (the toolBar and the buttons on it), are you creating them from Xcode by dragging and dropping or from pure code, therefore I will try to point out the common issues for both cases.
First, I am assuming that you are using Xcode and dragging the components that you like. In this case you need to create in the .h file an Outlet that will be linked to the button on the bar as follows:
#interface yourViewController : UIViewController
{
UIBarButtonItem *barButton;
}
#property (nonatomic, retain) IBOutlet UIBarButtonItem *barButton;
- (void) barButtonPress;
Notice that I added a function that will handle the bar button press. Now you need to link this Outlet to the bar button item, simply in Xcode in the Connection Inspector where it says New Referencing Outlet drag to the File's Owner box (the yellow cube).
Now in the viewDidLoad add the following:
[barButton setTarget:self];
[barButton setAction:#selector(barButtonPress)];
This code will link your bar button to the function that you want to be called when you press it. Now for the view that you like to view Modal, I assume that you already #import it also in the .h file, lets call it MyViewModal.
Inside the function that will be called when you press the bar button:
- (void) barButtonPress
{
MyViewModal *myViewModal = [[MyViewModal alloc] initWithNibName:#"MyViewModal" bundle:nil];
[self presentModalViewController:myViewModal animated:YES];
}
That's all, it will be displayed in Modal View. Keep in mind the allocating the new view is done based on your needs, here I did the simplest case just for illustration.
UPDATE: If not using Xcode
If you are not using Xcode then you should have a toolbar already defined say it is named myToolBar. To add buttoms to the tool bar we use the myToolbar.items way therefore we need to prepare the buttons with their targets before adding them. Here is a workflow:
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(barButtonPress) forControlEvents:UIControlEventAllEvents]; //same function as above
UIBarButtonItem *btn = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];
myTool.items = [NSArray arrayWithObjects:btn, nil];
This should do it for you.

UIActionSheet with UIPickerView and Done, Next, Previous buttons

i am trying to implement an action sheet that contains a picker view and a segmented control bar with a previous button, next button and done button like the image as follows http://imgur.com/8wVMy. I currently am able to make it look like this http://imgur.com/AXn6H. I was wondering if someone could help me get the picker view to sit on the bottom and just make it look a little better. Thanks for any help.
Unless you're targeting very old versions of iOS (i.e. versions earlier than 3.2), the best way to do it is to take a completely different approach.
As of 3.2, any UIResponder (which includes all UIViews) can return a UIView from its inputView property to show that view instead of the keyboard when the view becomes the first responder. This even works for views that normally don't become first responder or don't display the keyboard at all. It's simple:
Design your popup view, as you would any other view.
Ensure that your widget view returns YES from canBecomeFirstResponder.
Ensure that your widget view returns an instance of your popup view from inputView.
More details are available in the documentation.
Also, BTW, if you're on an iPad you should probably use a UIPopoverController to display a UIPickerView instead of either of these methods. Apple may actually require this if you intend to get your app in the app store.
The next and previous buttons are actually showing your images to segmentedController Within a toolbar. To get it You have to define the segmentedController and UIToolbar on. H. Next add the DataSource and UIPickerView
Then in the viewDidLoad create objects and define Their properties. For example:
if (keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
[keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];
segControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Anterior", #"Siguiente", nil]];
[segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segControl setTintColor:[UIColor blackColor]];
segControl.frame = CGRectMake(5, 7, 150, 33);
segControl.momentary = YES;
[segControl addTarget:self action:#selector(segSelected:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *aceptar = [[UIBarButtonItem alloc] initWithTitle:#"Hecho" style:UIBarButtonItemStyleDone target:self action:#selector(cerrarTeclado:)];
//aceptar.width = 70.0f;
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, aceptar, nil]];
[keyboardToolbar addSubview:segControl];
}

UIViewController: setToolbarItems vs navigationItem

my application has a UIViewController subclass which is being managed by a UINavigationController.
In the viewDidLoad of my UIViewController subclass, I was attempting to add a UIBarButtonItem to the toolbar like this:
settingsButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings"
style:UIBarButtonItemStylePlain target:self action:#selector(viewSettings:)];
[self setToolbarItems:[NSArray arrayWithObject:settingsButton]];
this wasn't working out for me, so after some googling around, I tried this:
[[self navigationItem] setRightBarButtonItem:settingsButton];
which worked out fine. from reading the UIViewController documentation, I'm still confused about why setToolbarItems wasn't working. I verified in the debugger that the button was in the toolbarItems array in the viewDidAppear method. the button itself just wasn't appearing on my toolbar.
so, my question is, why didn't setToolbarItems work for me in the first code snippet?
I don't have the toolbar configured in my xib for this view controller at all, if that makes a difference.
Yes that make the difference.Whenever you see a bar on view by default for navigation based apps that is not a toolBar actually that is , navigation bar.so you can add item by referencing self.navigationItem.
[self setToolbarItems:[NSArray arrayWithObject:settingsButton]]; essentially populates the navigation controller's bottom toolbar - not the Left and Right top bar buttons.
The bottom toolbar is, by default, not displayed. To display it you must call [self.navigationController setToolbarHidden:NO]
Below is the relevant documentation - UINavigationController Class Reference
toolbar:
The custom toolbar associated with the navigation controller.
(read-only)
#property(nonatomic,readonly) UIToolbar *toolbar Discussion This
property contains a reference to the built-in toolbar managed by the
navigation controller. 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.
Management of this toolbar’s contents is done through the custom view
controllers associated with this navigation controller. For each view
controller on the navigation stack, you can assign a custom set of
toolbar items using the setToolbarItems:animated: method of
UIViewController.
The visibility of this toolbar is controlled by the toolbarHidden
property. The toolbar also obeys the hidesBottomBarWhenPushed property
of the currently visible view controller and hides and shows itself
automatically as needed.
try to use
[toolbar setItems:[NSArray arrayWithObject:settingsButton] animated:YES];
instad of :
[self setToolbarItems:[NSArray arrayWithObject:settingsButton]];
shani
On ipad apps, you've got to set toolbar items to the "topViewController" (yes this is counter-intuitive).
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:catView];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"item 1" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"item 2" style:UIBarButtonItemStylePlain target:nil action:nil];
[nav setToolbarHidden:NO animated:YES];
// WRONG: [nav setToolbarItems:[NSArray arrayWithObjects:addButton, nil]];
// CORRECT (for ipad apps):
[nav.topViewController setToolbarItems:[NSArray arrayWithObjects:item1, item2, nil] animated:NO];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:nav];

Changing colours of a UIToolbar + it's buttons when presented in a Popover Controller on the ipad

On both the iPhone and the iPad I have a need to present two buttons on the right hand side of a navigation bar. I'm doing this with the following snippet of code:
UIToolbar *rightBarButtons = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 92, 44.01)];
UIBarButtonItem *send = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(send)];
[send setStyle:UIBarButtonItemStyleBordered];
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addRecipe:)];
[add setStyle:UIBarButtonItemStyleBordered];
NSArray *buttons = [[NSArray alloc] initWithObjects:send,add,nil];
[send release];
[add release];
[rightBarButtons setItems:buttons];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButtons];
[buttons release];
[rightBarButtons release];
On the iPhone the colours are fine, and in landscape mode on the iPad it is fine as they are grey. However in portrait mode the view appears inside a popover controller which has a dark black/blue colour. My buttons and the toolbar show up as the default grey.
How can I make the toolbar buttons match? If you do not use the hack above and just present one button as normal the colour change is handled and I guess I just need to implement that colour change manually, problem is, I can't seem to get the colour to change at all.
This would appear to be a property called barStyle and not tintColor as I previously thought. The simplest solution is to copy the bar style from elsewhere:
[rightBarButtons setBarStyle:self.navigationController.navigationBar.barStyle];
It's then fairly trivial to ensure the style remains correct as the view changes. Although I have to say I quite liked the dark blue black buttons over the silver navigation bar that gave after rotation.

How do you change color/image on the default backBarButtonItem?

I need to change color on the default "self.navigationItem.backBarButtonItem". To accomplish this I´ve created an custom Button class and implemented it like this:
- (void)viewDidLoad {
[super viewDidLoad];
BackButton *blueSaveButton = [[BackButton alloc] init];
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:blueSaveButton];
temporaryBarButtonItem.title = #"Tillbaka";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
[blueSaveButton release];
[self gotoLocation];
}
But this has no effect at all on the button.
So how do you manage to do this without "breaking" the default/inherited behavior of the navigationbar?
EDIT: The reason is that the button needs to be corporate branded, so default styles will not do.
BR
If you want the button to be blue (like a Done button) and have the title "Save" you can use one of the built in bar button item types:
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(myAction:)] autorelease];
Ok found the solution (not so good but it works) do this:
1: Implement the button from this tutorial: www.switchonthecode.com
This will override the default behavior of navigationbar.
2: Implement this in every view that needs the custom button:
osmorphis.blogspot.com
BR