Navigation bar right bar button item is not visible - ios

- (void)setRightNavigationBarViewForUser {
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
spacer.width = 760;
NSString *title = [VimondStore sessionManager].userName;
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 144, 44)];
tempView.backgroundColor = [UIColor clearColor];
UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 0, 44, 44)];
tempImageView.image = [UIImage imageNamed:#"user.png"];
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(44, 0, 80, 44)];
tempLabel.backgroundColor = [UIColor clearColor];
tempLabel.text = title;
tempLabel.font = [UIFont boldSystemFontOfSize:14.0];
tempLabel.textColor = [UIColor whiteColor];
tempLabel.textAlignment = NSTextAlignmentLeft;
tempLabel.adjustsFontSizeToFitWidth = YES;
tempLabel.minimumScaleFactor = 0.8;
[tempView addSubview:tempImageView];
[tempView addSubview:tempLabel];
UIBarButtonItem *userView = [[UIBarButtonItem alloc]initWithCustomView:tempView];
NSArray *items = #[spacer ,userView];
self.navigationTableViewController.navigationItem.rightBarButtonItems = items;
}
- (void)navigateToHome {
[self setRightNavigationBarViewForUser];
self.loginViewController = nil;
[self showCenterPanelAnimated:YES];
[self setLeftBarButtonForDrawerTitle];
NSAssert([self.centreViewController isKindOfClass:[GGBaseViewController class]], #"Must be of GGBaseViewController class");
[GenreNavigator navigateToRoot:(GGBaseViewController*)self.centreViewController completionHandler:nil];
}
My code is given above: The problem, I am facing is that navigation right bar button items are not visible first time when I navigate to home. When I navigate to some other page and comes back then its visible. The first method is used to create right navigation bar button items.

From Apple doc on rightBarButtonItems, you can see that most likely your custom view is too wide and your button is not showing because it does not fit. Test making it narrower and see if it appears?
Discussion
This array can contain 0 or more bar button items to display on the right side of the navigation bar. Items are displayed right-to-left in the same order as they appear in the array. Thus, the first item in the array is the rightmost item and other items are added to the left of the previous item.
If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the left side of the bar are not displayed.

Related

Can't set titleView in Navigation bar center and scope of back button increased

I am trying to push a view controller and set its navigation bar's title but it is not centered due to long title being used i guess. Along with that, scope of the back button is increased till the title view, i.e if I tap with with Milestone's "M", it gets back though it frame is the same.
Bounds of back button are the same but its click impact is elongated.
Below is the code for how I am pushing the view controller.
MilestoneDetailsViewController *controller = [[MilestoneDetailsViewController alloc] initWithNibName:#"MilestoneDetailsViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
and in the MilestoneDetailsViewController, i set the title as following:
self.title = NSLocalizedString(#"Milestone Details", nil);
Back button is picking its size according to the title of previous viewController. you can change it to a empty String for example, in your previous viewController from where you are pushing write this code.
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
Try this
UIView *tView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 160 ,44)];
UILabel *bartitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 4, tView.frame.size.width, 40)];
[bartitleLabel setBackgroundColor:[UIColor clearColor]];
[bartitleLabel setTextAlignment:NSTextAlignmentCenter];
[bartitleLabel setAdjustsFontSizeToFitWidth:YES];
[bartitleLabel setText:NSLocalizedString(#"Milestone Details!",#"")];
[self.navigationItem setTitle:#""];
[tView addSubview:bartitleLabel];
[tView setBackgroundColor:[UIColor clearColor]];
[self.navigationItem setTitleView:tView];
Try like this
UILabel* lbNavTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,40,320,40)];
lbNavTitle.textAlignment = UITextAlignmentCenter;
lbNavTitle.text = NSLocalizedString(#"Milestone Details!",#"");
self.title = lbNavTitle // here self.title is your navigation bar title

How to add multiple bar buttons to navigation bar

Initially i have created a dynamic view called 'mainView'.In that,i have added Navigation bar.After that, i Want to add 2 right bar buttons to the navigation bar.If i code like this,its shows view with navigation bar.but its not showing right bar button.
1.Is it possible to add navigation bar without navigation controller?
2.Why the right bar button is not showing?
//mainView Creation
UIView *mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
mainView.backgroundColor =[UIColor grayColor];
[self.view addSubview:mainView];
//NavigationBar
UINavigationBar *NavigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
NavigationBar.backgroundColor = [UIColor orangeColor];
[mainView addSubview:NavigationBar];
//rightbarView Creation
UIView *rightbarView = [[UIView alloc] init];
rightbarView.backgroundColor = [UIColor orangeColor];
//powerCut Button
UIButton *powerCutbutton = [[UIButton alloc] initWithFrame:CGRectMake(150, 20, 50, 14.01)];
[rightbarView addSubview:powerCutbutton];
UIImage *powercutimage = [UIImage imageNamed:#"powercut.jpg"];
[powerCutbutton setImage:powercutimage forState:UIControlStateNormal];
// addConnection button
UIButton *addConnectionbutton = [[UIButton alloc] initWithFrame:CGRectMake(210, 20, 50, 14.01)];
[rightbarView addSubview:addConnectionbutton];
UIImage *addimage = [UIImage imageNamed:#"add.png"];
[addConnectionbutton setImage:addimage forState:UIControlStateNormal];
// Barbutton item
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightbarView];
//[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:powerCutbutton, addConnectionbutton, nil]];
// setting rightbar button item
self.navigationItem.rightBarButtonItem = item;
create an array of bar buttons. and setLeftBarButtonItems not setLeftBarButtonItem as :
UIBarButtonItem * barbtn1;
UIBarButtonItem * barbtn2;
UIBarButtonItem * barbtn3;
[self.navigationItem setLeftBarButtonItems:#[barbtn1,barbtn2,barbtn3]];
or
[self.navigationItem setRightBarButtonItems:#[barbtn1,barbtn2,barbtn3]];
hope it will helpful for you

putting labels, buttons on the navigation bar iOS

I have created custom navigation controller,
I want to be added, a date at the left, a back button on the right and the title next to back button.
I tried to add one label, but it does not work. Please show me a way
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.text = #"My Text";
[self.navigationController.navigationBar addSubview:navLabel];
[self.view addSubview:naviBarObj];
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,8,200,30)];
navLabel.text = #"My Text";
navLabel.textColor = [UIColor redColor];
[naviBarObj addSubview:navLabel];
[navLabel setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:naviBarObj];
try this it will work .It works for me :)
add custom view in UIToolbar
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f,190.0f, 44.01f)];
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] init];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width =10.0f;
[buttons addObject:bi];
[buttons addObject:add your view];
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width =10.0f;
[buttons addObject:bi];
[buttons addObject:add your view];
[tools setItems:buttons animated:NO];
UIBarButtonItem *buttons_ = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = buttons_; //same to leftBarButtonItem
You can also achieve this from Interface Builder
1. First add a UIView which will look like following
2. Then add a UILabel inside the UIView which will look like following
3. In the Center It will look like following
Now add necessary constraint to meet your requirement.
You need to add navigation item to the navigation bar. One per each navigation level. So you should have at least one of them.
** update **
UINavigationItem * myNavItem = [[UINavigationItem alloc] initWithTitle:#"Some title"];
/*
* add buttons and labels
*/
[self.navigationController.navigationBar pushNavigationItem:myNavItem animated:NO];
You need to change the titleView of the topItem. Like this:
naviBarObj.topItem.titleView = navLabel;
** update: Complete code (tested and working) **
UINavigationBar *naviBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(.0, .0, 320.0, 44.0)];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem.titleView = titleLbl;
[naviBar pushNavigationItem: navItem animated: NO];
[navItem release]; // you don't need this for ARC
[self.view addSubview: naviBar];
[naviBar release]; // you don't need this for ARC

UIBarButtonSystemItemFlexibleSpace

Apple docs have this to say:
UIBarButtonSystemItemFlexibleSpace
Blank space to add between other items. The space is distributed equally between the other items. Other item properties are ignored when this value is set.
That's a little vague (exactly what space is distributed equally?) So I wrote a test method:
-(void) createToolbar {
BOOL stuffInTopLeftCorner = NO;
UIToolbar* bar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 1024, 44)];
self.bar = bar;
[self addSubview:bar];
UILabel* titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
titleLabel.text = #"Centered title";
titleLabel.font = [UIFont systemFontOfSize:30];
[titleLabel sizeToFit];
CGSize titleSize = [titleLabel bounds].size;
NSLog(#"titleSize is %g %g", titleSize.width, titleSize.height);
UIBarButtonItem* titleItem = [[UIBarButtonItem alloc]initWithCustomView:titleLabel];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UILabel* leftLabel = [[UILabel alloc]initWithFrame:CGRectZero];
leftLabel.text = #"Stuff in top left corner";
leftLabel.font = [UIFont systemFontOfSize:30];
[leftLabel sizeToFit];
UIBarButtonItem* topLeftCornerItem = [[UIBarButtonItem alloc]initWithCustomView:leftLabel];
NSMutableArray* items = [NSMutableArray arrayWithObjects: flexibleSpace, titleItem, flexibleSpace, nil];
if (stuffInTopLeftCorner) {
[items insertObject:topLeftCornerItem atIndex:0];
}
bar.items = items;
}
Here is what it looks like with the code as above:
And here is what it looks like if I change stuffInTopLeftCorner to YES:
It appears that adding something to the left of the title did not cause said title to move at all.
My question is -- does that mean it will always center the title, regardless of what goes on either side of it?
As best I can tell, yes, it always centers the title, assuming that is possible.

Why does navigationItem.titleView align left when presentmodalviewcontroller called?

I'm using a UILabel for the titleView of a navigation bar (I'm making simple in-app web browser). It works fine, except that when I present a modal view controller, the titleView shifts from the center of the navbar to the far left (underneath the back button). I've tested in 3.0 and up. Here is relevant code:
- (void)viewDidLoad {
[super viewDidLoad];
// Title view label
CGRect labelFrame = CGRectMake(0.0, 0.0, 120.0, 36.0);
UILabel *label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
label.font = [UIFont boldSystemFontOfSize:14];
label.numberOfLines = 2;
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(0.0, -1.0);
label.lineBreakMode = UILineBreakModeMiddleTruncation;
self.navigationItem.titleView = label;
}
-(void)displayComposerSheet:(NSString*)mailto
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
}
Screenshots:
Any idea why this is happening? Thanks.
I looked into the problem with some hit and try and found the following facts:
If the UINavigationBar doesn't have the rightBarButtonItem, the titleView shifts towards the right by ~30pts.
It could be reproduced for leftBarButtonItem. But I haven't tried.
In a scenario where the a default UINavigationBar's (with no changes to rightBarButtonItem defaults) titleView is set. And then a new UIView is pushed to the navigation stack which HAS a rightBarButtonItem. Now, if this view is popped [with back button], the navigation bar will remove the rightBarButtonItem. And this will account for the weird offset that shifts the titleView towards a side.
How I fixed the problem was like this:
self.navigationItem.titleView = myCustomTitleView;
// Fake right button to align titleView properly.
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 1)]];
// Width equivalent to system default Done button's (which appears on pushed view in my case).
rightBarButtonItem.enabled = NO;
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
Everything is sweet now. yummmm.
Thanks to DougW for pointing me in right direction. Here's the best hack I found. Basically I retain the UILabel as a class property. Before presenting modal view I unset the titleView, and then reset it immediately after. When the modal view is dismissed I unset then reset the titleView. To the user none of this is visibly notable.
-(void)displayComposerSheet:(NSString*)mailto
{
self.navigationItem.titleView = nil;
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
picker.navigationBar.tintColor = [APPDELEGATE getNavTintColor];
[picker setToRecipients:[NSArray arrayWithObject:mailto]];
[self presentModalViewController:picker animated:YES];
[picker release];
self.navigationItem.titleView = titlelabel;
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
self.navigationItem.titleView = nil;
self.navigationItem.titleView = titlelabel;
[self dismissModalViewControllerAnimated:YES];
}
Does it animate? It may be animating the title view as though it's transitioning to a new view. I don't see anything wrong with your code as written.
I would suggest in your displayComposerSheet, you just unset the titleView, or animate the alpha of the titleView to 0.0. Then, animate it back to 1.0 when you dismiss the modal view controller. Not ideal, but it may look better that way.
Frankly, the whole UINavigation system is crap. We went ahead and re-wrote it ground up because of bizarre issues like these.
The only problem is your frame size. so u have to change it.
Try this one.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 36.0)];
label.font = [UIFont boldSystemFontOfSize:14];
label.numberOfLines = 2;
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(0.0, -1.0);
label.lineBreakMode = UILineBreakModeMiddleTruncation;
label.text=#"Stack Overflow";
self.navigationItem.titleView = label;
You can try move the code in viewDidAppear:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// You code to customize title view
self.navigationItem.titleView = logoImage;
}
It works for me.
If you change the width size to be small like 100 points or smaller instead of 120 you set, this problem may go away. Setting width of the label smaller worked for me.
UIView *view= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[view setUserInteractionEnabled:NO];
view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"logo_small.png"]];
UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:view ];
self.navigationItem.leftBarButtonItem = barButton;

Resources