navigationbar, title, center alignment through code - ios

I have a number of of xibs where I manipulate the title of the navigation bar through code:
titleLabel.text = #"custom Title";
titleLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = titleLabel;
The problem I have is that in some cases I add to that bar a right or left button, or both, and whenever I do that the text alignment ends up being a little bit off and not centered proerly. How can I fix that?

Get the width of the barButton and subtract it from the label's width.
You can determine your bar button's frame through this method:
Get the width of a UIBarButtonItem
i.e.
NSInteger barButtonWidth = //determined through method above ^^^^^
titleLabel.frame = CGRectMake(titleLabel.frame.origin.x, titleLabel.frame.origin.y, titleLabel.size.width-barButtonWidth, titleLabel.size.height);

I kind of found out a workaround it.
By adding the the buttons before setting the title view, xcode automatically sets the alignment properly. the order is important.

This worked for me
UILabel *labelNav;
- (void)viewDidLoad {
[super viewDidLoad];
labelNav=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 20)];
labelNav.font=[UIFont fontWithName:boldFont size:15];
labelNav.textColor=barTintColor;
labelNav.text=kBoostBusinessHoursScreenTitle;
labelNav.textAlignment=NSTextAlignmentCenter;
labelNav.center=self.navigationController.navigationBar.center;
CGRect framelabel=labelNav.frame;
framelabel.origin.y=12;
labelNav.frame=framelabel;
[self.navigationController.navigationBar addSubview:labelNav];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[labelNav removeFromSuperview];
}

Related

NavigationItem LeftBarButtonItem Custome View Auto Layaut

I am trying to set a costume navigationItem.leftBarButtonItem from a nib, but i get an weird behaviour in landscape.
EDIT:
To be clear the view contains a button and a label,this is why i am using a costume view, both button and label have constraints.
self.buttonView =//init;
[self.buttonView setFrame:CGRectMake(0, 0, 36, 36)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.buttonView];
[self.buttonView setBackgroundColor:[UIColor redColor]];
//you will see why
Seems that autolayout is trying to satisfy some constraints thus its expanding your button's frame.
To fix this set the contentHugging priority to required for your button:
[self.buttonView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
Try that and let me know if it works
This is the only workaround i could find asap, if someone have a better idea please feel free to share
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
[self setMenuButtonFrame];
}
-(void)setMenuButtonFrame
{
self.menuView.frame = CGRectMake(self.menuView.frame.origin.x,
self.menuView.frame.origin.y,
self.navigationController.navigationBar.frame.size.height-8,
self.navigationController.navigationBar.frame.size.height-8);
}

Can't remove UILabel from UINavigationController.view

I'm adding a tiny marker under my UINavigationController title so the user will know that the title is tappable. You can see in the code below how I add this label to the navigation bar.
_labelCalendarMenuArrow = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 5, 30, 10, 26)];
_labelCalendarMenuArrow.text = #" ̬";
_labelCalendarMenuArrow.font = [UIFont fontWithName:#"HelveticaNeue" size:30];
_labelCalendarMenuArrow.textAlignment = NSTextAlignmentCenter;
_labelCalendarMenuArrow.textColor = [UIColor whiteColor];
[self.navigationController.view addSubview:_labelCalendarMenuArrow];
The problem is that I'm unable to remove this UILabel from the navigationController.view when leaving this screen. In the code below you can see how I try a few methods for hiding or removing this UILabel, but none of them work... The UILabel will stay in the NavigationController until I go to a different stack of views and come back. Any advice?
- (void)viewWillDisappear:(BOOL)animated {
[_labelCalendarMenuArrow removeFromSuperview];
_labelCalendarMenuArrow = nil;
_labelCalendarMenuArrow.alpha = 0;
}
A simple solution can be to use HIDDEN property
- (void)viewWillDisappear:(BOOL)animated
{
_labelCalendarMenuArrow.hidden=YES;
}
What you are trying to do here is fairly horrific, adding a view within a parent navigation controller's view is against all sense.
Please read apple's human interface guidlines as there is a better solution to signifying that the title is clickable in there. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/
If you still insist on adding a label underneath the navigation bar's title label you should implement a titleView for the navigation item. Within that view you will have to include your own title label to replace the original and then your signifier label underneath.

Remove back button text and center title in navigation bar

I've removed the text for UIBarButton in AppDelegate:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -1000.f) forBarMetrics:UIBarMetricsDefault];
Which works like a charm:
As you can see, this doesn't align the navigation title at horizontal center. What is the best solution to accomplish this globally for all views.
PS: I am using Storyboard.
You can create your own custom titleView with a UILabel as follows:
UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0,0,100,50)];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,100,50)];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = #"PAKKELISTE";
[titleView addSubview:titleLabel];
self.navigationItem.titleView = titleView;
The details of the frames, text, alignment, etc. are just an example. The main idea though is that you set a custom UIView as the navigationItem's titleView.
It could also be an issue with your back button offset. Try this approach instead for removing the "back" text (I haven't tried this before, but I'm curious if it will work).
self.navigationController.navigationBar.topItem.title = #"";

Move contents of UINavigationBar

I found through another question on here how to change the height of UINavigationBar using a category:
#import "UINavigationBar+Taller.h"
#implementation UINavigationBar (Taller)
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(self.frame.size.width,100); //exaggerated height
return newSize;
}
#end
That works brilliantly.
Is there a way to move the title and navigation buttons up? They are aligning to the bottom of the new size. I want to put something into the bottom of the navigation bar.
Private methods are ok since this isn't going to be an app store candidate.
To change the vertical offset for the title:
CGFloat yourOffset = 5.0; //whatever value you need
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:yourOffset forBarMetrics:UIBarMetricsDefault];
To make the button a bit higher or lower
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Another (more flexible) better approach would be to create a subclass of UINavigationBar
and override layoutSubviews, placing your views in the correct place.

Table footer view buttons

I have some signup form that had email and login text fields as table cells and signup button as button in footer view.
That all functioned superb, here is the code
frame = CGRectMake(boundsX+85, 100, 150, 60);
UIButton *signInButton = [[UIButton alloc] initWithFrame:frame];
[signInButton setImage:[UIImage imageNamed:#"button_signin.png"] forState:UIControlStateNormal];
[signInButton addTarget:self action:#selector(LoginAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.hidesBackButton = TRUE;
self.tableView.tableFooterView.userInteractionEnabled = YES;
self.tableView.tableFooterView = signInButton;
self.view.backgroundColor = [UIColor blackColor];
My question is how to add another button next to this one.
By using [self.tableView.tableFooterView addSubview:...] buttons are not shown and if I use some other UIView, place buttons there and then say that the footerView is that UIView, I see the buttons, but am unable to press them.
I hope that this isn't too confusing and that you understand my problem.
Take a look at this question: UIButtons on tableFooterView not responding to events
your first try is wrong, if you are doing as you as saying you are trying to add a button to the subview of a button:
first you
...
self.tableView.tableFooterView = signInButton;
...
and then later
...
[self.tableView.tableFooterView addSubview:...]
...
but tableFooterView is signInButton. So that is why that is not working.
your second try is correct and the answer yonanderson pointed you should work out and is the correct way to do this, you just need to :
[yourButtonsView addSubView:button1];
[yourButtonsView addSubView:button2];
yourButtonsView.userInteractionEnabled = YES;
self.tableView.tableFooterView = yourButtonsView;
self.tableView.tableFooterView.userInteractionEnabled = YES;
Humm, I didn't read this close enough before answering. I think you need to set the container UIView's userInteractionEnabled property then as you tried, set the footerView to the container with the subviews.
It took me forever to figure out why my footer's buttons were not calling the designated actions. Finally I discovered that I needed to adjust my footer's height. This fixed it for me:
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return footerView.bounds.size.height;
}

Resources