I have a problem with my navigation bar. I'm setting a custom font, and the centering is not right, the back button it's moving everything to the right.
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 100)];
navLabel.backgroundColor = [UIColor clearColor];
navLabel.text = dataFromOtherView.text;
navLabel.textColor = [UIColor whiteColor];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0];
navLabel.shadowOffset = CGSizeMake(0, 0);
navLabel.font = [UIFont fontWithName:#"Trebuchet MS" size:22.0];
navLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = navLabel;
It looks like the label is so large that there is not enough 'open/flexible space' between the left side of the label and the right side of the back button. Try reducing the width of UILabel when you create it.
Related
I'm trying to have two UILabels in my navigation bar instead of just one.
I followed this link to have informations on how to do that:
iPhone Title and Subtitle in Navigation Bar
It works well, but I can't get my texts to be centered properly.
It is centered between the buttons, but the default title behaviour is to center itself right under the time.
I had a look here, same question, but no answer:
UINavigationBar TitleView with subtitle
What am I missing?
Here is my code:
CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44);
UIView* _headerTitleSubtitleView = [[UILabel alloc] initWithFrame:headerTitleSubtitleFrame];
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor];
_headerTitleSubtitleView.autoresizesSubviews = NO;
CGRect titleFrame = CGRectMake(0, 2, 200, 24);
UILabel *titleView = [[UILabel alloc] initWithFrame:titleFrame];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20];
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
titleView.shadowColor = [UIColor darkGrayColor];
titleView.shadowOffset = CGSizeMake(0, -1);
titleView.text = #"Title";
titleView.adjustsFontSizeToFitWidth = YES;
[_headerTitleSubtitleView addSubview:titleView];
CGRect subtitleFrame = CGRectMake(0, 24, 200, 44-24);
UILabel *subtitleView = [[UILabel alloc] initWithFrame:subtitleFrame];
subtitleView.backgroundColor = [UIColor clearColor];
subtitleView.font = [UIFont boldSystemFontOfSize:13];
subtitleView.textAlignment = NSTextAlignmentCenter;
subtitleView.textColor = [UIColor whiteColor];
subtitleView.shadowColor = [UIColor darkGrayColor];
subtitleView.shadowOffset = CGSizeMake(0, -1);
subtitleView.text = #"Subtitle";
subtitleView.adjustsFontSizeToFitWidth = YES;
[_headerTitleSubtitleView addSubview:subtitleView];
self.navigationItem.titleView = _headerTitleSubtitleView;
You should adjust the width of both frames. It should be below 200. try this.
CGRect titleFrame = CGRectMake(0, 2, 160, 24);
CGRect subtitleFrame = CGRectMake(0, 24, 160, 44-24);
Edit : Your backbutton on the left is wider, and the titleview is shifted to the right.
Please look the image with width 200px
And the image with width 160px
I suggest you to adjust the width of titleview and label accordingly.
If you want to know more about backbutton width, then please refer the discussion here.
SO Post 1.
SO Post 2.
you may like this property in UINavigationItem class.
#property (nonatmic,copy) NSString *prompt
It's elegant.
I would like to be able to control the position of the title in a UInavigationBar. For example, right now my titles are centered, but for some views, I need to shift the title origin a few px to the left or so. Is this possible?
Thank you!
You can add a custom titleView to your navigation item, and place an off-center UILabel in it to hold your title. Another easier way, might be to just append a space character or two to the end of your title if that gives you the amount of displacement you need.
your code here
- (void) customNavBarTitleWithText:(NSString *) text andFontsize:(float) fontsize
{
// Custom nav bar back button
UIImage *img = [UIImage imageNamed:#"back_btn.png"];
UIButton *backbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
[backbtn setImage:img forState:UIControlStateNormal];
[backbtn addTarget:self action:#selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc] initWithCustomView:backbtn];
[self.navigationItem setLeftBarButtonItem:backBarBtn];
//Custom nav bar title
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:fontsize];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = NSTextAlignmentCenter;
// Use UITextAlignmentCenter for older SDKs.
label.textColor = [UIColor whiteColor]; // change this color
self.navigationItem.titleView = label;
label.text = text;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0;
[label sizeToFit];
// Set nav bar background image
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"top_bar_bg.png"] forBarMetrics:UIBarMetricsDefault];
}
in there:
label.textAlignment = NSTextAlignmentCenter;
you can use another mode: NSTextAlignmentLeft, NSTextAlignmentRight, NSTextAlignmentJustified, etc..
Hope it work for you!
In my UINavigationBar I have a multi line title, I am using the following the code to setup:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 50)];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 2;
label.font = [UIFont boldSystemFontOfSize: 15.0f];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = #"Headloss due to Friction (Hazen Williams Eq.)";
self.navigationItem.titleView = label;
Since this is on a sub-view it has a "Back" button, added by the UINavigationController. So I figured that by adding the label.textAlignment = NSTextAlignmentCenter; my title would be centered, but it's actually centering my title from the end of my "Back" button to the end of my screen.
How can I override this so that my title is actually centered based on my screen dimension, (basically, the way it would be by default should the back button not be present).
Good question that's pretty tough. If I find a more elegant solution I'll edit my answer but this is what I came up with:
UIView *spaceView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithCustomView:spaceView];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObject:space];
And just adjust the width of the UIView for how far you want your titleLabel to move.
I have a UICollectionViewController and trying to set navigation title..
but none of these works...It does not show the text!
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.titleLabel.backgroundColor = [UIColor clearColor];
self.titleLabel.font = [UIFont fontWithName:#"ArialHebrew-Bold" size:15];
self.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.textColor = [UIColor whiteColor];
self.titleLabel.text = [self.navigationItem title];
self.navigationItem.titleView = self.titleLabel;
[self.titleLabel setText:#"title"];
self.navigationItem.title = #"The title";
Make the frame of your new label bigger. The height of a nav bar is fixed at 44 pixels.
For example: CGRectMake(100,0,120,44);
The width and origins depend on what else is on the navbar, and the width of the navbar.
I'm not having much luck centering vertically the label I'm adding to the TitleView on the UINavigationBar. You can see what it looks like below.
This is how I'm adding the label:
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = NSLocalizedString(#"activeSessionsTitle",#"");
titleLabel.font = [Util SETTING_NEO_HEADER_FONT];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f];
titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);
[titleLabel sizeToFit];
super.navigationItem.titleView = titleLabel;
I think the reason why it's doing this is there is something weird with the actual font I'm using -- as I've had to do a lot of repositioning inside of buttons and such. I've been able to solve the issue everywhere but the navigation bar.
Opening up an old Question, but I found this to be very useful.
iOS 5 allows your to use [UINavigationBar appearance] which is great for changing the Title bar's title view without the need for a custom UIView:
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"HelveticaNeue" size:25.0f],
UITextAttributeFont,
nil]];
Sometimes, depending on the UIFont that you use, the title view may be vertically off.
To fix this, you can use:
[self.navigationBar setTitleVerticalPositionAdjustment:(float) forBarMetrics:UIBarMetricsDefault];
(float) being a positive value to push the titleView down, and a negative value to push the titleView up.
I hope this helps.
I ended up using a combination of the answer RPM left and one of the comments on my question. This is what ultimately fixed it for me:
UIView *customTitleView = [[UIView alloc] init];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 3.0f, 200.0f, 30.0f)];
titleLabel.text = titleString;
titleLabel.font = [Util SETTING_NEO_HEADER_FONT];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f];
titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);
[titleLabel sizeToFit];
customTitleView.frame = CGRectMake(self.navigationItem.titleView.frame.size.width/2 - titleLabel.frame.size.width/2, self.navigationItem.titleView.frame.size.height/2 - titleLabel.frame.size.height/2, titleLabel.frame.size.width, titleLabel.frame.size.height);
[customTitleView addSubview:titleLabel];
[titleLabel release];
[self.navigationItem setTitleView:customTitleView];
[customTitleView release];
If you're using this chunk of code, you may have to play with the "y" value on the title label's initWithFrame: call. I have it set to 3.0f, but you may have to adjust it a bit for your own usage.
The reason the other solutions didn't seem to work for me is they would go off-center horizontally depending if I had one barbutton (left or right). If there were no bar buttons it was fine, and if there were two it was fine. But one would cause it to go off-center.
I dont think this has anything to do with your font. I have also used titleView and I think the way it lays out the view in a weird way and doesnt take into consideration what your view size is.
You could also set the frame of your label view as such
titleLabel.frame = CGRectMake(self.navigationItem.titleView.frame.size.width/2 - titleLabel.frame.size.width/2, self.navigationItem.titleView.frame.size.height/2 - titleLabel.frame.size.height/2, titleLabel.frame.size.width, titleLabel.frame.size.height);
self.navigationItem.titleView = titleLabel;
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName:UIFont(name:"Exo2-Bold", size: 18) as! AnyObject,NSForegroundColorAttributeName:UIColor.whiteColor()]