Add UIImage or UILabel to navigationItems - ios

In my app, I have to customize my navigation bar depending on the page I'm on. In some views, I need to add some UIButton, but also some UIImage (for the logo that is not centered) or some UILabel.
I could do that using [self.navigationController.navigationBar addSubview:myLabel]. But I don't really like this method because if I ever have to add a UIButton or just change the text of the UILabel, I need to change manually the origin of each UILabel and each UIImage.
I thought of using something that already exists : UIBarButtonItem. It can contain some text, an image, and I just need to add each of them in the navigationItem.leftBarButtonItems, and every thing is nicely positioned.
The only problem that remains, is that it still is a button, and reacts as a one when pressed. If I disable it, it's alpha changes to show it's disabled... How could I keep it's normal apparence, and let it disabled ?
Thanks !
PS: I'm open to any other idea to add UIImages or UILabels in a navigation bar :)

Try this code snippet. You can change Label and imageView frame and put as your requirement.
UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 200, 16)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = #"My first lable";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.text = #"second line";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
imgviw.backgroundColor = [UIColor blackColor];
imgviw.image=[UIImage imageNamed:#"abc.png"];
[btn addSubview:imgviw];
self.navigationItem.titleView = btn;
Hope that it helps.

You can do this by not setting any #selector for created button, and setting the same customized properties for both UIControlStateNormal and UIControlStateSelected. Also, set tintColor to ClearColor. Like that, button will still be a button, it will stay there, always look the same but it won't do anything.
Hope it helps.

you can use barButton created with customView.
for example,
UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 100, 40)];
[btn setTitle:#"some Title" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor greenColor]];
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:btn];

Related

mulpitle UILabel in Navigation titleView [duplicate]

I am developing one application for iphone and in that i have some issues regarding adding more then one UILabel in navigation bar at a specific position.
What i want is in following images
in above image there is one backbar button and one imageview as shown with arrow-mark. Other then that all white box is for difrent UILabels to show in navigation bar.
There is only one UIImageView in the navigation bar and one left bar button. Now problem is that i don't know how to add multiple UILabel in navigation bar at a specific position.
Till now what i have worked is to add only button and UIImageView with the right bar button array or left bar button array, but that only add items in sequnce.
So can anyone please guide me that how can anyone add UILabels or any other item at a specific position..
you can add Multiple UIlabel or Image as look like bellow image:-
this can do using bellow code you can change Label and imageView frame and put as your requirement
- (void)viewDidLoad
{
UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 200, 16)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = #"My first lable";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.text = #"second line";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
imgviw.backgroundColor = [UIColor blackColor];
imgviw.image=[UIImage imageNamed:#"a59117c2eb511d911cbf62cf97a34d56.png"];
[btn addSubview:imgviw];
self.navigationItem.titleView = btn;
}
You can add subviews directly to the navigationBar -
UINavigationBar *navBar = navController.navigationBar;
[navBar addSubview: yourLabel];
yourLabel frame origin will be relative to the navigation bar
Try this code
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
buttonContainer.backgroundColor = [UIColor clearColor];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setFrame: CGRectMake(160, 7 , 40, 30)];
[button0 setTitle:#"Sort" forState:UIControlStateNormal];
button0.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:12];
[button0 setBackgroundImage:[UIImage imageNamed:#"Btn_Sort_Btn_Sort_Places.png"] forState:UIControlStateNormal];
[button0 addTarget:self action:#selector(sortAction) forControlEvents:UIControlEventTouchUpInside];
[button0 setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
self.navigationItem.titleView = buttonContainer;
UILabel *lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0 , 140, 40)];
lbl_title.text = str_HeaderTitle;
lbl_title.textColor = [UIColor whiteColor];
lbl_title.font = [UIFont fontWithName:#"Helvetica-Bold" size:16];
lbl_title.backgroundColor = [UIColor clearColor];
lbl_title.textAlignment = NSTextAlignmentCenter;
[buttonContainer addSubview:lbl_title];
You can set an arbitrary view instead of a view controller's title:
myViewController.navigationItem.titleView = someView;
Note that most likely the view's frame will be restricted to make room for leftBarButtonItem and rightBarButtonItem.
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(50, 2, 20, 15)];
[label setBackgroundColor:[UIColor greenColor]];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(40, 20, 150, 10)];
[imgView setBackgroundColor:[UIColor redColor]];
[[self.navigationController navigationBar] addSubview:label];
[self.navigationController.navigationBar addSubview:imgView];
Have a look at this piece of code. With some modifications it should solve your problem:
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setFrame:CGRectMake(15, 0, 57, 44)];
[imageButton setBackgroundImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 0, 250, 44)];
[titleLabel setText:#"some title"];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[self.navigationController.navigationBar addSubview:imageButton];
[self.navigationController.navigationBar addSubview:titleLabel];
Just modify the values in the CGRectMake() to fit your needs.

Button Text not showing

When I try to post the following code, my app just shows an empty rectangle where the button's text should be. I am a beginner at xcode and I have determined this code from other tutorials.
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *player1 = [[UILabel alloc]initWithFrame:CGRectMake(35, 120,130, 30)];
[player1 setText:#"Player"];
player1.layer.borderWidth = 1.0;
player1.layer.borderColor = [UIColor blackColor].CGColor;
player1.textAlignment = UITextAlignmentCenter;
[self.view addSubview: player1];
UIButton *score1 = [[UIButton alloc]initWithFrame:CGRectMake(165, 120, 60, 30)];
[score1 setTitle:#"0" forState:UIControlStateNormal];
score1.layer.borderWidth = 1.0;
score1.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview: score1];
UILabel *player2 = [[UILabel alloc]initWithFrame:CGRectMake(35, 150, 130, 30)];
[player2 setText:#"Opponent"];
player2.layer.borderWidth = 1.0;
player2.layer.borderColor = [UIColor blackColor].CGColor;
player2.textAlignment = UITextAlignmentCenter;
[self.view addSubview: player2];
}
Like I said, everything but the text in the button is showing up.
The reason why the button looks blank is because its text color is actually white. Just give it a different color and 0 will now show:
score1.layer.backgroundColor = [UIColor blackColor].CGColor;
If you don't want to change the button background color, you may want to set the text color:
[score1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Read Question: [initWithFrame:frame] vs. [UIButton buttonWithType], for more about creating a UIButton with these different methods.
The problem is the way you're creating the button:
UIButton *score1 = [[UIButton alloc]initWithFrame:CGRectMake(165, 120, 60, 30)];
That's not how you do it. Do it like this:
UIButton *score1 = [UIButton buttonWithType:UIButtonTypeSystem];
score1.frame = CGRectMake(165, 120, 60, 30);
All will be well after that!

Title of a Custom UIBarButtonItem from a UIButton not showing

I made a custom UIBarButtonItem from a UIButton. The custom bar button is visible, the action works, the size is correct, and the background color can be seen; however the manually set title cannot be seen. maybe background is laid over the text? I'm not sure, help?
In viewDidLoad:
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:#selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:#"ProximaNova-Bold" size:19];
resetButton.titleLabel.text = #"RESET";
resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;
UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];
self.navigationItem.rightBarButtonItem = resetBarBTN;
You should set the title for button of using setTitle:forState: for particular control states, replace your code with below
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:#selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:#"ProximaNova-Bold" size:19];
[resetButton setTitle:#"RESET" forState:UIControlStateNormal]; //change this line in your code
resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;
UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];
self.navigationItem.rightBarButtonItem = resetBarBTN;
Use setTitle:forState to set button title. For example
[resetButton setTitle:#"text" forState:UIControlStateNormal]

Shifting the text of UINavigationBar title left or right

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!

Centering UINavigationBar title text issue

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.

Resources