Navigation title gets hidden by bar button - iphone

In my app when I'm setting the title more than 13 characters it is hidden by bar button.So how do i overcome from this problem.Please help me.Thanks in advance.

Use a smaller font or a shorter title, or use a multiline title by customizing the navBar's titleView with a custom UILabel. For example,
// CUSTOMIZE NAVIGATION BAR TITLE LABEL
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:20.0]];
[label setAdjustsFontSizeToFitWidth:YES];
[label setTextAlignment:UITextAlignmentCenter];
[[self navigationItem] setTitleView:label];
[label release];
You can customize it however you like.

Related

Navigation title is not shown properly in iphone SDK

As you see my image, the Navigation title is under the two buttons.
The two left/right buttons are added subviews to navigation Bar.
How can i set the navigation title width?? Eventhough it looks like ...
My code is as follow:
self.navigationItem.titleView.frame=CGRectMake(0, 0, 20, 40);
[[self navigationItem]setTitleView:bigLabel];
If your UILable is too long you can set navigation bar Title like this way iniphone` but in ipad you can use same logic :-
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setNumberOfLines:0];
[label setTextColor:[UIColor whiteColor]];
[label setTextAlignment:UITextAlignmentCenter];
[label setFont:[UIFont systemFontOfSize:12]];
NSString *str =#"1234567890 1234567890 1234567890 1234567890 1234567890 123456";
[label setText:str];
self.navigationItem.titleView = label;
}
Hope its helps you .. :)
Use Custom UIView with one button one switch and one title. Either make it by Xib or make it by code. And Hide default navigation bar.
Try to change the x position of the added subviews.
Make that something like this:
Yoursubview.frame=CGRectMake(self.view.size.width-Yoursubview.frame.size.width,0,Yoursubview.frame.size.width,Yoursubview.frame.size.height);

align UILabel to be centered on UIView

I am implementing a footer view, which is a UIView and it has a label inside it. I wanted it so that the UILabel stays in the center all the time, but it just won't. Here's my code:
- (void)setupFooterView
{
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView_.frameWidth, kFooterViewHeight)];
[footerView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth];
[footerView setBackgroundColor:[UIColor clearColor]];
UILabel *loadMoreLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[loadMoreLabel setBackgroundColor:[UIColor clearColor]];
[loadMoreLabel setFont:[UIFont fontWithName:kProximaNova size:16]];
[loadMoreLabel setTextColor:[UIColor whiteColor]];
[loadMoreLabel setLineBreakMode:UILineBreakModeTailTruncation];
CGSize desiredSize = [[PNRConstants kLoadingMore] sizeWithFont:loadMoreLabel.font constrainedToSize:CGSizeMake(footerView.frameWidth, footerView.frameHeight) lineBreakMode:loadMoreLabel.lineBreakMode];
[loadMoreLabel setFrameSize:desiredSize];
[loadMoreLabel setCenter:CGPointMake(footerView.frameWidth/2, footerView.frameHeight/2)];
self.loadingMoreLabel_ = loadMoreLabel;
[footerView addSubview:self.loadingMoreLabel_];
[loadMoreLabel release];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[spinner setBackgroundColor:[UIColor clearColor]];
[spinner setFrameX:self.loadingMoreLabel_.frameX - spinner.frameWidth - 10];
[spinner setFrameY:self.loadingMoreLabel_.frameY];
self.spinner_ = spinner;
[footerView addSubview:self.spinner_];
[spinner release];
[self.scrollView_ setFooterView_:footerView];
[footerView release];
}
What am I doing wrong here?
[loadMoreLabel setCenter:CGPointMake(footerView.frameWidth/2, footerView.frameHeight/2)];
The footerView.frameWidth/2 needs to be
footerView.frameWidth/2 - loadMoreLabel.frame.size.width/2
This is because the anchor of the label is in the top left corner instead of in the center. You subtract half the width of your label to account for this.
after setting label's frame: [yourLabel setCenter: yourFooter.center];
The right way: use nibs, set the resize mask and call it a day.
The painful way: programmatically create the label.
Then a couple of options:
- make the label as wide as the footer and set it's text align to center.
- set the label's center property to footer.frame.width/2 and footer.frame.height/2
Edit: oh, you're dong that already. Set the text align property of yournlabel to center.

UINavigationBar custom title position

I have a custom made UINavigationBar (different size, background etc) that has inside a custom title view. I used this code to achieve this:
UIView * mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
mainView.backgroundColor = [UIColor yellowColor];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,kScreenWidth,80)];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UINavigationItem *currentItem = [[UINavigationItem alloc] init];
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont fontWithName:#"Helvetica-Bold" size: 30.0];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:self.title];
[label sizeToFit];
[currentItem setTitleView:label];
[label release];
[navBar pushNavigationItem:currentItem animated:NO];
[currentItem release];
[mainView addSubview:navBar];
[navBar release];
self.view = mainView;
[mainView release];
However, my problem now is that, even if the custom title view is correctly added, it's not vertically centered with the NavBar. What I am missing?
Thanks!
You can use setTitleVerticalPositionAdjustment:forBarMetrics: in iOS 5 to change the title position, it works on normal title e.g.:
CGFloat verticalOffset = -4;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
You created you UINavigationBar with a height of 80 points, when a standard UINavigationBar has 44 pts. It probably position it centred it on a 44 pts based centre...
You can try to make a UIView with a height of 80 pts and add the UILabel centred inside it. (not tested, just a guess)

Set NavigationBarButton Title Text Color

I just wanted to know if it is possible to set the NavigationBarButton's title's text color, and if so how?
I did the following. In -(id)init add:
// CUSTOMIZE NAVIGATION BAR TITLE COLOR
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:20.0]];
[label setAdjustsFontSizeToFitWidth:YES];
[label setTextAlignment:UITextAlignmentCenter];
[[self navigationItem] setTitleView:label];
[label release];
Then in -(void)viewDidLoad add:
[[[self navigationItem] titleView] setText:#"TITLE"];
[[[self navigationItem] titleView] setTextColor:titleColor];
I put them like this so that I could change the title and titleColor based on how one got to this screen. If you don't need to customize, then put it all in init or viewDidLoad. Doing it like this I get a warning that "UIView may not respond to '-setText'" even though it all works fine.
You can set an image for the UIBarButtonItem and thus fully customize the title text.

Setting truncation mode for UINavigation title text

I want to configure the truncation mode for the text that appears in UINavigationItem.
I've come across a couple of different solutions of configuring the font size but not sure how to make use of the UILineBreakModeHeadTruncation that exists on NSString.
How I've been able to configure the font size:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:self.title];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];
(Based on the recommendation from here)
If I call this in loadView or viewDidLoad, it seems to work but outside of that method, after the view has been loaded, it doesn't seem to work. Not sure why though.
What have you tried, and what's not working? You should be able to do:
label.lineBreakMode = UILineBreakModeHeadTruncation;
Or to wrap to multiple lines:
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;