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).