0

I want to set a text on the position of leftBarButtonItem/rightBarButtonItem in the UINavigationBar. You have to use UIBarButtonItem, but you can put different UIView objects into it. First I tried to use a label, but that doesn't work. The code is in C#, but you should get the idea:

UILabel textLabel = new UILabel();
textLabel.Text = "My custom text which should be displayed in navigation bar";
UIBarButtonItem customBarButtonItem = new UIBarButtonItem (textLabel);
NavigationItem.RightBarButtonItem = customBarButtonItem;

The label is not shown. If I use a button it seems to work (except the styling has to be adapted).

UIBarButtonItem customBarButtonItem = new UIBarButtonItem ("My custom text which should be displayed in navigation bar", UIBarButtonItemStyle.Plain,null);
NavigationItem.RightBarButtonItem = customBarButtonItem;

Why isn't it possible to use a UILabel? What is the correct way of showing a text like a header in a UIBarButtonItem?

Edit:

My current findings are:

Either set the title of UIBarButtonItem or set the frame of UILabel (thanks to Anbu.Karthik for this).

2
  • You better show the real code and stick to naming conventions. This is rather guess work. Commented Sep 7, 2015 at 8:18
  • @HermannKlecker: I edited my question to have some more meaningful placeholders. The code is what I want to use later. Only the content would change. Commented Sep 7, 2015 at 8:48

3 Answers 3

1

change

UILabel testLabel = new UILabel();
testLabel.frame=CGRectMake(0, 0, 200, 21);

testLabel.Text = "yuhu";
UIBarButtonItem test = new UIBarButtonItem (testLabel.text);
Sign up to request clarification or add additional context in comments.

8 Comments

else check the label frame is necessary or not
The only constructor I see is the one I used in my question (you need some more parameters). FIrst, I tried to use auto layout which leads to a crash because he doesn't know the parent. Now I tried to set the frame and the label gets displayed! How would the same thing work with auto layout? Where do I have to add the constraints? The View of UINavigationController?
if you use the label in directly in your view then you can thing about auto layout , but UIBarbutton does not underwith in any chain hierarchy , so in here only need to move/assign the label text to bar button text , so label frame is fine for here
If I understood you correctly I can safely set the frame and forget auto layout for this case (because it Isn't in any view hierarchy)?
in here if you try to use auto layout , surely your app will crash the reason is not a UIButton , UIButton has chain hierarcy , but UIBarbutton contains not
|
1

Change this line of code UIBarButtonItem test = new UIBarButtonItem (testLabel);

UILabel testLabel = new UILabel();
testLabel.Text = "yuhu";
 testLabel.frame=CGRectMake(x, y, width, height);
//change this line

UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:testLabel];
NavigationItem.RightBarButtonItem = test;

8 Comments

If I translate that into the C# world this is exactly the same line UIBarButtonItem test = new UIBarButtonItem (testLabel); if btn is testLabel. Or do you mean UIButton?
I only want to display a text. The first idea for this is to use UILabel, but that didn't worked for me.
but a label will not give any meaning to bar button as it will not have any action.are u planning to add to tap gesture to uilabel
It should only display a text. No action required here.
i don't thin so constraint will work, U may have to go frame based
|
0

Use this

UIImage *buttonImage = [UIImage imageNamed:@"back.png"];
 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 [button setImage:buttonImage forState:UIControlStateNormal];
 button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
 [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

 UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
 self.navigationItem.leftBarButtonItem = customBarItem;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.