UINavigationBar custom title position - iphone

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)

Related

Adding a subview to another subview is not working

I am trying to add label (headerLabel) to another subview (introPadTutorial) here and it is not showing up on the screen. Can some please help me where am I making mistake?
//Header file
#property (nonatomic, retain) UIView *introPadTutorial;
//Implementation file
#synthesize introPadTutorial;
UIView *v = [_appDelegate getCurrentView];
CGRect tutorialFrame = [self getTableViewFrame];
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width, 80, -tutorialFrame.size.width, v.frame.size.height)];
[introPadTutorial setBackgroundColor:[UIColor uberLightGray]];
[introPadTutorial setUserInteractionEnabled:YES];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 180)];
[headerLabel setText:#"test test test"];
[headerLabel setBackgroundColor:[UIColor greenColor]];
[headerLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
[headerLabel setTextColor:[UIColor redColor]];
[headerLabel setShadowColor:[UIColor whiteColor]];
[headerLabel setShadowOffset:CGSizeMake(0, 1)];
[self.introPadTutorial addSubview:headerLabel];
[headerLabel release];
[v addSubview:introPadTutorial];
[introPadTutorial release];
Don't use a negative width to change the POSITION, use the origin to change the position.
Use this:
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width - tutorialFrame.size.width, 80, tutorialFrame.size.width, v.frame.size.height)];
I think that should help make the subview actually show up.
May be your...
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width, 80, -tutorialFrame.size.width, v.frame.size.height)];
should actually be...
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(0, 80, tutorialFrame.size.width, v.frame.size.height)];
Is your introPadTutorial view showing up? You have a negative width in it's initWithFrame call.

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.

image in title of navigation bar in objective c

I have made an iPad application, in that I used navigation control,
now in the title bar , I want to put image on left side, so I hide title bar with label, but label is not covering entire width of title bar,
IL APP IN THE SCREEN SHOT,
here is the code snippet,
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 800, 50)];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.textColor = [UIColor yellowColor]; // Change to desired color
titleView.backgroundColor=[UIColor blackColor];
titleView.text = #"IL APP";
titleView.textAlignment=UITextAlignmentCenter;
//self.navigationItem.titleView = titleView;
[self.navigationItem setTitleView:titleView];
[titleView release];
}
#Gama as far as your question is concerned you are asking to put an Image but your real issue being described is that the label is not covering up the title bar. To cover that properly you can use
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 32)];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.textColor = [UIColor yellowColor]; // Change to desired color
//titleView.backgroundColor=[UIColor blackColor];
titleView.text = #"IL APP";
titleView.textAlignment=UITextAlignmentCenter;
[self.navigationItem setTitleView:titleView];
[titleView release];
}
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
For using an image you can use an image view instead of a label to be assigned as the titleView for navigationItem.
Hope it helps
I tried to solved your problem but I get the same result.
But you can hide navigation bar and add the UILabel in to your ViewController
self.navigationController.navigationBarHidden = FALSE;
[self.view addSubview:titleview];
UIImageView *imageNav = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"navImage.png"]];
[self.navigationController.navigationBar addSubview:imagenav];
[self.navigationController.navigationBar sendSubviewToBack:imageNav];
[imageNav release];
Hi i wanted to do the same thing and i saw your posting, i basically used some of the code you provided and modified it a little and got it to work see below:
//declare and initialize your imageView
UIImageView *titleImage = (UIImageView *)self.navigationItem.titleView;
titleImage = [[UIImageView alloc]
initWithFrame:CGRectMake((self.navigationController.navigationBar.frame.size.width/2)
- (100/2), 0, 100, self.navigationController.navigationBar.frame.size.height)];
//setting the image for UIImageView
titleImage.image = [UIImage imageName:#"photo.jpg"];
//NOTE: you have to do this line at the bottom to add image to the navigation bar Try it it WORKS!
self.navigationItem.titleView = titleImage;

How do I vertically correct the navigationBar's titleView text position when using a custom font?

We're using custom fonts for the titleView in the navigation bar.
Somehow Apple always draws this font too high.
How do I correct for this strange offset you get when you are using custom fonts in a navbar?
I used setTitleVerticalPositionAdjustment:forBarMetrics:.
Compatibility: available starting from iOS 5.
Your can set a new view as titleView, then add a new label to it:
UIView * customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 40.0f)];
UILabel * customLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 200.0f, 20.0f)];
[customLabel setBackgroundColor:[UIColor clearColor]];
[customLabel setTextColor:[UIColor whiteColor]];
[customLabel setFont:[UIFont systemFontOfSize:12.0f]];
[customLabel setTextAlignment:UITextAlignmentCenter];
[customLabel setText:#"Your Text"];
[customTitleView addSubview:customLabel];
[customLabel release];
[self.navigationItem setTitleView:customTitleView];
[customTitleView release];

ContainerView hide UILabel

I want to use UILabel in ContainerView.
So I am using this code for that.
UILabel *myLabel = [[[UILabel alloc] initWithFrame:CGRectMake(16, 60, 300, 150)] autorelease];
myLabel.numberOfLines = 0;
myLabel.font = [UIFont systemFontOfSize:13.5];
myLabel.text = [theQuiz objectAtIndex:row+3] ;
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.layer.cornerRadius = 8.0;
[myLabel sizeToFit];
[self.view addSubview:myLabel];
//ContainerView
UIView *ChallengeView = [[UIView alloc] initWithFrame:CGRectMake(8, 55, 300, 10 + Challenge.frame.size.height)];
ChallengeView.layer.borderColor = [[UIColor purpleColor ] CGColor];
[ChallengeView setBackgroundColor:[UIColor whiteColor]];
ChallengeView.layer.cornerRadius = 8 ;
ChallengeView.layer.borderWidth = 1.5;
[self.view addSubview:ChallengeView];
[ChallengeView release];
Now problem is that when i set background color for ContainerView it hides the text of myLabel
Any Solution ??
What is happening is that you containerView is being added above label either you add label after containerView or do this:
[self.view bringSubviewToFront:myLabel];
You add the ChallengeView first , Then add the myLabel.
Otherwise u can do as like #xs2bush said,
[self.view bringSubviewToFront:myLabel];
Bcz the ChallengeView hides the label.