I created a nav bar and a label programmatically in my view controller. Now I want to add a "done" button but cant seem to find a way without using IB....is there any way to do that?
Here is my view controller's viewDidLoad:
//Adding navBar programmatically
CGFloat width = self.view.frame.size.width;
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,width,52)];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:navBar];
//Adding label to navBar programmatically
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,2,width-20,14)];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.text = #"TITLE";
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:22];
label.shadowOffset = CGSizeMake(1,1);
label.textColor = [UIColor whiteColor];
label.textAlignment = UITextAlignmentCenter;
[navBar addSubview:label];
//Adding back button to navBar programmatically
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(dismissView:)];
self.navigationItem.leftBarButtonItem = rightButton;
Last part obviously doesnt work...
Thanks for any help!
Why are you not using a UIToolBar instead?
To add buttons to a UINavigationBar, you need to create a UINavigationItem. If you're using UINavigationBar as part of UINavigationController (as is the common case), this is done automatically, as a standalone view, you have to do this yourself.
UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:#""] autorelease];
item.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(dismissView:)] autorelease];
[navBar setItems:[NSArray arrayWithObject:item] animated:NO];
The navigationItem is only used when you push a viewcontroller in a navigationcontroller
in your case you need to use
-pushNavigationItem:animated:
on the UINavigationBar
Check the docs for more details:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UINavigationBar_Class/Reference/UINavigationBar.html
Related
I am attempting to set a label into a subclassed UIToolBar. I am first creating the label, then converting it to a UIBarButtonItem and setting them to the toolbar, but the toolbar just appears blank.
here is my code in my init method of my subclassed toolbar
NSMutableArray *items = [[NSMutableArray alloc] init];
// prep the total label
_totalLabel = [[UILabel alloc] init];
_totalLabel.font = [_totalLabel.font fontWithSize:15];
_totalLabel.textColor = [UIColor whiteColor];
_totalLabel.text = NSLocalizedString(#"TOTAL", nil);
_totalLabel.frame = CGRectMake(0, 0, 100, 44);
_totalLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
_totalLabel.layer.borderWidth = 2;
_totalLabel.layer.borderColor = [UIColor greenColor].CGColor;
_totalLabel.backgroundColor = [UIColor clearColor];
UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spacer2];
UIBarButtonItem *total = [[UIBarButtonItem alloc] initWithCustomView:_totalLabel];
[items addObject:total];
[self setItems:items];
any idea whats wrong?
Where are you calling this code from? If you have it in initWithFrame: or initWithCoder: it will not work.
Try this:
- (void)layoutSubviews {
[super layoutSubviews];
if(self.items.count == 0) {
//Your code here
}
}
That will get it working. The question is though should you be doing this in layoutSubviews? Probably not. You could also put a setup method on your UIToolbar and call that at a later point in your application.
This is not a uitoolbar. this is a navigation bar so please remove navigationbar and add toolbar . Try this type .
I need it aligned to left. Or there is no way and I need to add custom label?
Add it to the left button item:
UIView *mCustView = [[UIView alloc] initWithFrame:CGRectMake(0,0,200,20)];
mCustView.backgroundColor = [UIColor redColor];
UILabel *mTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,20)];
mTextLabel.backgroundColor = [UIColor blueColor];
mTextLabel.font = [UIFont systemFontOfSize:20];
mTextLabel.textColor = [UIColor blackColor];
mTextLabel.text = #"Random text tested here, so sit back and enjoy";
[mCustView addSubview:mTextLabel];
[mTextLabel release];
UIBarButtonItem *mCustomBarItem = [[UIBarButtonItem alloc] initWithCustomView:mCustView];
self.navigationItem.leftBarButtonItem = mCustomBarItem;
[mCustView release];
You need to add a custom view to the navigation bar's -titleView
self.navigationItem.titleView = someLabel;
I think you could use UIBarButtonSystemItemFixedSpace which provides a way of adding a fixed padding in a navigation bar:
UIBarButtonItem *fixedSpaceItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:NULL]
autorelease];
fixedSpaceItem.width = 50;
UIBarButtonItem *cancelItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:nil
action:NULL]
autorelease];
// vc is some view controller
vc.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:
fixedSpaceItem,
cancelItem,
nil];
This will right indent the "Cancel" button 50 points.
I have found the solution by myself:
float offset = 210.0f;
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, offset, 44.01)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
I want add a UIToolbar to the keyboard for my UITextField. Here is the code I'm using:
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.tintColor = [UIColor blackColor];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(someFunction)];
doneButton.tintColor = [UIColor blackColor];
UISegmentedControl *directionControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Back", #"Next", nil]];
directionControl.tintColor = [UIColor blackColor];
directionControl.segmentedControlStyle = UISegmentedControlStyleBar;
[directionControl addTarget:self action:#selector(directionControlPressed) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segItem = [[UIBarButtonItem alloc] initWithCustomView:directionControl];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:segItem, flexibleSpace, doneButton, nil]];
// Assign the toolbar to the text fields
self.textField.inputAccessoryView = toolbar;
However, here is what it looks like when I run the code:
The toolbar doesn't seem to be high enough; also I notice that the tint colour of the toolbar hasn't been acknowledged.
Please can someone help me out?
Think you need to set the toolbar's frame, at least its size. As I recall, I used [toolbar sizeToFit] to get the height and had to use the window width to get the width.
I am trying to create a view that looks like the following:
I know it's a UISplitView, however my issue is that on creating the UIToolBar that is below the UITableView to the left and also on how to create the icons on top of the detail view (the one with the trash can logo, reply, and new message). How do I do this?
If you look at the DetailView.xib for a UISplitView project in Interface Builder you can drag UIBarButtonItems onto the toolbar contained in it.
To get a toolbar in the popover controller you can just use the toobar that comes with a UINavigationController. The code below should give you an idea of how to do this. Add this code to RootViewController.
- (void)viewDidLoad
{
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
//setup toolbar
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
[self setToolbarItems:[NSArray arrayWithObjects:refreshItem, nil]];
[refreshItem release];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60.0, 10.0, 200.0, 21.0)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = #"This is a label on the toolbar";
[self.navigationController.toolbar addSubview:label];
[label release];
}
Hello Everyone
I am writing a simple code in ViewDidLoad, and class is child of UITableViewController, like below but buttons are not visible, while title is visible,
Another thing that I am clicking a button which in ViewDidLoad method ViewController.m, and which is call a method, code of that method is as below
//Code of button target method
-(void)statusMethod {
NSLog(#"statusMethod");
Status *ob=[[Status alloc]init];
[self presentModalViewController:ob animated:YES];
}
//Code of ViewDidLoad of Status.m
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, width, 43)];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:navBar];
[navBar release];
UIBarButtonItem *home = [[UIBarButtonItem alloc] initWithTitle:#"HOME" style:UIBarButtonItemStylePlain target:self action:#selector(homeMethod)];
self.navigationItem.rightBarButtonItem = home;
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithTitle:#"Add" style:UIBarButtonSystemItemAdd target:self action:#selector(addMethod)];
self.navigationItem.leftBarButtonItem = add;
UILabel *label = [[UILabel alloc] initWithFrame:
CGRectMake(10,10,width-20,25)];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.text = #"Status";
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:25];
label.textAlignment = UITextAlignmentCenter;
[navBar addSubview:label];
[label release];
Any Idea, what is problem in code?
I you don't understand, then u can ask me again,
I will praise, if I will get solution of my problem
It seems like self.navigationItem is only looked at by the UINavigationController (controller, not bar). The bar itself isn’t going to check that.
So you should either
1) Use a real UINavigationController which comes with its own navigationBar and will handle the navigationItems for you. The self.navigationItem code above will work in that case. (you should consider self.navigationItem.title = #"Status";)
If your UITableViewController is going to be pushing stuff on and off the navigation stack, this is the path you should take in any case.
2) Use the UINavigationBar’s own navigation item. Except it doesn’t seem to come with a navigation item, so you have to add your own:
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Status"];
[navBar setItems:[NSArray arrayWithObject:navItem]];
[navItem release];
and then set the left and right buttons as
[navBar topItem].leftBarButtonItem = add;
[add release];
[navBar topItem].rightBarButtonItem = home;
[home release];
Dealing with UINavigationController/UINavigationBar/UINavigationItem can be confusing, but luckily Apple has a decent explanation of how all these things work together at the top of its UINavigationController documentation.