Cant set title or button inside UINavigationController - objective-c

I inherit from UINavigationController now I want to set a button and my title in ViewDidLoad.
- (void)viewDidLoad
{
[super viewDidLoad];
self.toolbar.barStyle = UIBarStyleBlackOpaque;
self.navigationBar.barStyle = UIBarStyleBlackOpaque;
[self.navigationItem setTitle:#"TEST"];
[self setToolbarHidden:YES];
}
But i doesn't work, I think its something I miss.

For set title of UINavigationBar
use
self.title = #"TESTING";
and BarButton on NavigationBar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(showActionsheet)] autorelease];

Related

Trying to add BarButtonItem to programmatically created NavigationController

I have pushed a view controller embedded in a navigation controller after tapping a button in my app. I am trying to add a "SAVE" button in the right corner of the nav bar. However, with this code it is not showing up. My assumption is that it is not showing up because nc.navigationItem.rightBarButtonItem should be placed in viewWillAppear. Since nc is programmatically created and does not have a class file with viewWillAppear how can I do this?
Here is my code:
- (IBAction)editProfileButtonTapped:(UIButton *)sender {
FXFormViewController *vc = [[FXFormViewController alloc] init];
vc.formController.form = [[PersonalContactInfoForm alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
nc.navigationBar.translucent = NO;
nc.navigationBar.barTintColor = [UIColor colorWithRed:(105/255.0)
green:(210/255.0)
blue:(231/255.0)
alpha:1.0];
// WHY IS THIS BUTTON NOT BEING ADDED?
nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:nil];
[self presentViewController:nc animated:YES completion:nil];
}
The UINavigationController uses the -navigationItem of the currently displayed view controller. Try this instead:
vc.navigationItem.rightBarButtonItem = ...
Here you can take advantage of a powerful feature of OOP called Inheritance.
Create a Class say MyFXFormViewController inherited from FXFormViewController.
MyFXFormViewController.h
#import "FXFormViewController.h"
#interface MyFXFormViewController : FXFormViewController
#end
MyFXFormViewController.m
#import "MyFXFormViewController.h"
#implementation MyFXFormViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.rightBarButtonItem = saveBarButton;
}
And your previous code should be like
- (IBAction)editProfileButtonTapped:(UIButton *)sender {
MyFXFormViewController *vc = [[MyFXFormViewController alloc] init];
vc.formController.form = [[PersonalContactInfoForm alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
nc.navigationBar.translucent = NO;
nc.navigationBar.barTintColor = [UIColor colorWithRed:(105/255.0)
green:(210/255.0)
blue:(231/255.0)
alpha:1.0];
[self presentViewController:nc animated:YES completion:nil];
}

Resizing UIToolbar when UISearchBar becomes active

I have an UIToolbarwith 5 items:
UIBarButtonItem with image
UIBarButtonItem flex width
UIBarButtonItem with custom view (UISearchBar)
UIBarButtonItem flex width
UIBarButtonItem with image
When I select the UISearchBar, I get it to keep its correct size and become active. However, I would like the image to the left and to the right to disappear and give the UISearchBar the full width of the UIToolbar. How do I do that?
This is what I have:
https://dl.dropbox.com/u/3077127/demo_1.mov
And this is what I want:
https://dl.dropbox.com/u/3077127/demo_2.mov
This is my code:
#pragma mark View lifecycle
- (void)viewDidLoad {
[...]
SearchBar *mySearchBar = [[SearchBar alloc] init];
mySearchBar.delegate = self;
[mySearchBar sizeToFit];
[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[mySearchBar setTintColor:[UIHelpers getSlColor]];
CGRect searchBarFrame = mySearchBar.frame;
searchBarFrame.size.width = 218;
[mySearchBar setFrame:searchBarFrame];
UIView *searchBarContainer = [[UIView alloc] initWithFrame:mySearchBar.frame];
[searchBarContainer addSubview:mySearchBar];
[searchBarContainer setTag:99];
UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"location-arrow"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"location-arrow"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];
NSArray *items = [[NSArray alloc] initWithObjects:left, flex, search, flex, right, nil];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolbar setItems:items];
[toolbar setTintColor:[UIHelpers getSlColor]];
self.tableView.tableHeaderView = toolbar;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectNull];
[...]
[super viewDidLoad];
}
#pragma mark -
Here is my answer to a similar question posted here
The key to a correct animation is to specify UIViewAnimationOptionLayoutSubviews as an option for the animation.
Not sure if you have tried this, but maybe you can use the UISearchBarDelegate methods to get notified when the user clicks on the search bar.
Example:
#pragma mark - UISearchBarDelegate Methods
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
// Remove the right and left buttons from the ToolBar
[self updateToolBarWithImages:NO];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
// Add back the right and left buttons to the ToolBar
[self updateToolBarWithImages:YES];
}
#pragma mark - Private Methods
// Custom method used to Update the ToolBar
- (void)updateToolBarWithImages:(BOOL)iShowImages {
// Logic to update ToolBar based on the BOOL value in showImages
// use "setItems:animated:" method of UIToolBar to set the ToolBar Items
[toolBar setItem:items animated:YES];
}
You shouldn't use a ToolBar for that.
Simply use a NavigationBar, with your 2 right and left ButtonItems, plus add to the NavigationBar's titleView a SearchBar with the appropriate delegates.
So you don't have to resize your NavigationBar. It's just about adding/removing the NavigationBar's right and left ButtonItems animated like so:
if (self.searchBar.isActive) {
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
[self.navigationItem setRightBarButtonItem:nil animated:YES];
}
else {
[self.navigationItem setLeftBarButtonItem:self.leftButton animated:YES];
[self.navigationItem setRightBarButtonItem:self.rightButton animated:YES];
}
Hope this helps!

How to add UIBarButtonItem to QLPreviewController

I already read this question QLPreviewController remove or add UIBarButtonItems but it's not what I'm looking for. I would like to keep the "Print" button in the navigation Bar but also add a new "Delete Document" button in the navigation bar.
I tried this:
QLPreviewController *previewer = [[[QLPreviewController alloc] init] autorelease];
[previewer setDataSource:self];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]initWithTitle:#"Salva Documento" style:UIBarButtonItemStyleBordered target:self action:#selector(saveFileToDocuments)];
NSArray *buttons = [NSArray arrayWithObjects:[[previewer navigationItem]rightBarButtonItem],saveButton, nil];
[[previewer navigationItem]setRightBarButtonItems:buttons];
But it didn't work.
Because you said "4.x will be fine", there's your problem.
The documentation for UINavigationItem [setRightBarButtonItems: animated:] (documentatin linked there for you) say that this function only works on iOS 5.0 and newer.
It will not work on iOS 4.0.
Also, you should add an animated: parameter to that setRightBarButtonItems: call.
UIBarButtonItem *rbb;
-(void)addRightButton{
if (!rbb) {
UIButton *orderButton = [UIButton buttonWithType:UIButtonTypeCustom];
orderButton.frame = CGRectZero;
rbb = [[UIBarButtonItem alloc] initWithCustomView:orderButton];
}
self.navigationItem.rightBarButtonItem = rbb;
}
- (void)viewDidLoad{
[super viewDidLoad];
[self performSelector:#selector(addRightButton) withObject:nil afterDelay:0.2];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self addRightButton];
}

UIButton UIPopUpController programmatically

having difficulty getting UIButton Items to show.. I want to place several UIButtons within my UIPopUpController. I'm doing everything in a storyboard and I need to do this section programmatically.
- (void)showPopover:(id)sender
{
if(![popoverController isPopoverVisible])
{
myPopOver = [[PopUp alloc] init];
popoverController = [[UIPopoverController alloc] initWithContentViewController:myPopOver] ;
[popoverController setPopoverContentSize:CGSizeMake(300, 200.0f)];
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
CGRect rect = (CGRect)[sender frame];
rect.origin.y = self.view.frame.size.height - rect.size.height;
[popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
{
[popoverController dismissPopoverAnimated:YES];
}
}
You could use a UINavigationController (it has already a navigation bar where you can attach buttons items) and add it as the content of your UIPopoverController. The UINavigationController can be istantiated using the initWithRootViewController method. In this case the controller would be your Popup class. For example, inside your showPopover method, you could do the following:
PopUp* myPopOver = [[PopUp alloc] init];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:myPopOver];
popoverController = [[UIPopoverController alloc] initWithContentViewController:navController];
// do other stuff here to present you UIPopoverController
[myPopOver release];
[navController release];
Now, inside your PopUp class, within viewDidLoad method, you can customize the UINavigationController navigationBar. For example:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *aButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(somethingSelector:)];
self.navigationItem.rightBarButtonItem = aButtonItem;
[aButtonItem release];
}
where somethingSelector is like the following:
- (void)somethingSelector:(id)sender
{
// your custom actions
}
EDIT
Alternatively you can avoid to use the UINavigationController and create UIToolbar inside your PopUp class. Inside the UIToolbar you can attach UIBarButtonItems. See UIToolbar Class Reference

Buttons on top of UIPopoverController

I want to add two Buttons on top of UIPopoverController like it is shown in following screenshots:
HTML Edit
Thanks for helping me!
Add your view controller to a UINavigationController, then add the Navigation Controller to the UIPopoverController. Then in your UIViewController's viewDidLoad method, put this code in:
UIBarButtonItem *okButton = [[UIBarButtonItem alloc] initWithTitle:#"Ok" style:UIBarButtonItemStyleBordered target:self action:#selector(okayButtonPressed)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelButtonPressed)];
self.navigationItem.title = #"My Title";
[self.navigationItem setLeftBarButtonItem:cancelButton animated:NO];
[self.navigationItem setRightBarButtonItem:okButton animated:NO];
[cancelButton release];
[okButton release];
You need to initialize your popover with a UINavigationController directly. Then set the root view to your custom view controller.
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
Use a UINavigationController as the pop-over. Then, access the .navigationBar property of the navigation controller, get the .topItem, and set its .leftBarButtonItem and .rightBarButtonItem.
I wouldnt use a navigationcontroller like the previous posters suggested, apple recommends not using navigationcontrollers on ipad (with good reason) it doesnt behave as youd expect when pushing VC into the stack when used in popovers, now you dont really want to use the "navigation" aspect of it, but i wouldnt use navigationcontroller just because uw ant the bar....Use a UIToolBar instead, and set its buttons to whatever you want...no need to use a navigation controller here...
When I do this my navBar doesn't seem to fit properly inside of the UIPopoverController, as shown in the below:
http://www.flickr.com/photos/coleorton/4752223066/
Here's what I'm doing:
// alloc the Direct Reports view controller.
ToolsViewController *toolsViewController = [[[ToolsViewController alloc] init] autorelease];
UINavigationController *toolsNavController = [[[UINavigationController alloc] initWithRootViewController:toolsViewController] autorelease];
toolsNavController.title = #"Tools";
toolsNavController.view.frame = CGRectMake(0.0, -10.0, 320.0, POPOVER_HEIGHT);
if(![self.toolsPopoverController isPopoverVisible]){
// show popover
self.toolsPopoverController = [[[UIPopoverController alloc] initWithContentViewController:toolsNavController] autorelease];
self.toolsPopoverController.delegate = self;
self.toolsPopoverController.popoverContentSize = CGSizeMake(320.0, POPOVER_HEIGHT);
[self.toolsPopoverController presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
} else {
// close popover
[self.toolsPopoverController dismissPopoverAnimated:YES];
}
This worked!
//Determine how to present this view based on device
if ([UIDevice currentDevice ].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
BNRAssetTypeViewController *contentViewController = [[BNRAssetTypeViewController alloc] init];
UINavigationController *popOverNavigation = [[UINavigationController alloc] initWithRootViewController:contentViewController];
self.assetPickerPopover = [[UIPopoverController alloc] initWithContentViewController:popOverNavigation];
[self.assetPickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
then in the init function of contentViewController add this
//add a barbutton item which will help in adding new type
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addNew:)];
//set bar item to right side of navbarite
self.navigationItem.rightBarButtonItem =bbi ;