label is not visible in UIToolBar - iphone

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 .

Related

How to change UINavigationBar's label position (frame)?

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];

can I add a UIBarButtonItem completely IB free?

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

creating apple ipad mail view

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];
}

What to do when UIBarButtonItem are not visible in UINavigationBar?

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.

UINavigationBar BarButtonItem with Plain Style

i got the following code:
- (id)init {
if (self = [super init]) {
self.title = #"please wait";
UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"star.png"] style:UIBarButtonItemStylePlain target:self action:#selector(buttonFavoriteClicked:)];
self.navigationItem.rightBarButtonItem = favorite;
}
return self;
}
but my button looks still like a Button with UIBarButtonItemStyleBordered
is there a way to set a button with plain style at this position?
Create a UIButton in interface builder, make it look like exactly what you want. Create an outlet for in in your .h:
IBOutlet UIButton * _myButton;
Then set it as your right bar button item using a custom view, which will eliminate the border:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_myButton];
This works because UIButton is a subclass of UIView.
Try this instead:
- (id)init {
if (self = [super init]) {
self.title = #"please wait";
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[button setImage:[UIImage imageNamed:#"star.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonFavoriteClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];
self.navigationItem.rightBarButtonItem = favorite;
}
return self;
}
Hope it helps.
Or do this in code without IB:
// add setting button to nav bar
UIImage* settingsGearImg = [UIImage imageNamed:#"settings_gear.png"];
CGRect frameimg = CGRectMake(0, 0, settingsGearImg.size.width, settingsGearImg.size.height);
UIButton *settingsButton = [[UIButton alloc] initWithFrame:frameimg];
[settingsButton setBackgroundImage:settingsGearImg forState:UIControlStateNormal];
[settingsButton addTarget:self action:#selector(settingsButtonAction) forControlEvents:UIControlEventTouchUpInside];
[settingsButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *settingButtonItem =[[UIBarButtonItem alloc] initWithCustomView:settingsButton];
self.navigationItem.leftBarButtonItem = settingButtonItem;
Results in:
When using this icon image (it's white on a white background so isn't visible here - link is: http://i.stack.imgur.com/lk5SB.png):
It's weird but it works. Say "No" to images/outlets! You shouldn't even set the UIBarButtonItemStylePlain property. The trick is to place button into UIToolBar with some special attributes:
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Cool plain bar"];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.01f)];
tools.clipsToBounds = NO;
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshTableView)];
[buttons addObject:bi];
[tools setItems:buttons animated:NO];
UIBarButtonItem *rightButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
item.rightBarButtonItem = rightButtons;
item.hidesBackButton = YES;
[myCoolNavigationBar pushNavigationItem:item animated:NO];
Although Frank is right, this code should be in viewDidLoad, the issue here is what BarButtons look like. If you want it to look different, you need to use a different type of UIView. You can add a UIButton to a UIToolbar just fine.
-(void)viewDidLoad {
[super viewDidLoad];
self.title = #"please wait";
self.navigationItem.rightBarButtonItem = [[[UIButton alloc] init] autorelease];
}
Edit:
Sorry, you wanted a plain UIBarButtonItem. I don't believe you can do this with a UINavigationBar. You could try adding a UserInteractionEnabled UILabel as the rightBarButtonItem, I'm not sure if it will work or not.
Seems this isn't currently possible.
sbwoodside has a solution.
Why not try UI7Kit? Works well for me, easy to use.
UIImage *img = [UIImage imageNamed:#"white_star.png"];
CGSize itemSize = CGSizeMake(20, 20);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[img drawInRect:imageRect];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIBarButtonItem *barButton = [[UIBarButtonItem alloc ] initWithImage:img style:UIBarButtonSystemItemAdd target:self action:#selector(favoriteButtonClicked)];
barButton.style = UIBarButtonItemStyleBordered;
Try this,
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"more.png"] style:UIBarButtonItemStylePlain target:self action:#selector(didPressButton)];