Set the title of the UINavigationBar - objective-c

Is it possible to set self.navigationItem.title = #"MyTitle";after the view has loaded ?
I am using a search bar and in the same view I feed a UITableView using the search results. So I want to set the search bar's text value as the title of the navigation bar, after the search results loaded.
I am using iOS 4.3.

Just set the title of UINavigationBar like this:
navBar.topItem.title = #"sample string";

Found a solution. I followed Pedro Valentini's post.
This is what I did,
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
label.textAlignment = UITextAlignmentCenter;
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:text];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];

Normally the titleView will be an UILabel, so you can fetch it, cast it, and set its text.
For some reason I had problems using this in viewDidLoad, adding it to viewWillAppear seemed to do the trick.
((UILabel *)self.navigationController.navigationBar.topItem.titleView).text = #"your title here"

Yes. You can dynamically set the title of the navigationItem during any part of the lifecycle of a UIViewController. The set operation is handled by a getter method that sends a message to the UINavigationController to upload the navigation bar's view contents to reflect the new title.

That should work if self in "inside" a UINavigationController.

_navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
_navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UINavigationItem *navItem = [UINavigationItem alloc];
navItem.title = #"Title";
Try This.

self.navigationItem.title = #"Terms and Conditions";

Related

UINavigationBar setTitleTextAttributes

I'm trying to change the color of NavigationBars title while I'm on a viewcontroller (not before pushing it). by using:
[[[self navigationController] navigationBar] setTitleTextAttributes:textAttributes];
But this line of code only works before pushes or pops. I was wondering if there is a way to force this without navigating?
I would say the simplest way is to create an UILabel with the same style you want for the UINavigationController title and set the label to the navigationItem.titleView.
Try this one
UILabel * label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,45,45)] autorelease];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.text = self.navigationItem.title;
self.navigationItem.titleView = label;
create custom label and set it as titleView of your navigation bar

Adding list to a view without changing views

How can I add a list (TableView) to a view without changing views, I want to show the list over the current view but in the middle of the view, so the original view will be showed also.
I tried with label...but gettign some problems adding tableView instead of label.
UIView *testView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 100, 100)autorelease];
testView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.2f]; UILabel *label = [[[UILabel alloc] init] autorelease];
label.text = #"TEST";
[label sizeToFit];
[testView addSubview:label];
[self addSubview:testView ];
Refer mjpopupviewcontroller link as it is according to your requirement

UITextField Not Allowing Input

I am programatically adding a UITextField into a UIImageView. When I set the text property, I can see the text.
The issue is, it's not letting me input text into it like an input field when I run the app.
This is my code for initializing the TextField:
UITextField *textField = [[UITextField alloc] init];
textField.frame = CGRectMake(38.0f, 0.0f, self.view.frame.size.width - 38.0f, 40.0f);
textField.text = #"Hello";
textField.background = [[UIImage imageNamed: #"field.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:19];
textField.backgroundColor = [UIColor whiteColor];
textField.userInteractionEnabled = YES;
[bar addSubview: textField];
Do I need to do something special to allow for text input?
If bar is an UIImageView, try to set
bar.userInteractionEnabled = TRUE;
Text Field not added into imageview . You can add textfield on view,for user interaction enabled.
First you can add imageview and then add text field on the view.
Like This--
[self.view addSubview:imgV];
[self.view addSubview: textField];

Change Text Lable to Image in XCode

I have the following code in my XCode project for an iOS app I'm developing:
testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
UIFont *Font = [UIFont fontWithName:#"Arial" size:40];
[testLabel setFont:Font];
[testLabel setTextAlignment:UITextAlignmentCenter];
[testLabel setTextColor:[UIColor colorWithRed:(float) 55/255 green:(float) 41/255 blue:(float) 133/255 alpha:1.0]];
testLabel.text = #"Here We Go";
I am looking to put an image in that spot instead of the text. What do I need to replace this code with?
Either you make an image and put it in an UIImageView or you make a UIView subclass in which you will draw the text inside the drawRect method.
In the second case, in your drawRect you do this :
[self.yourStringProperty drawAtPoint:CGPointMake(100,150) withFont:[UIFont systemFontOfSize:14.0]];
or
[self.yourStringProperty drawInRect:rect withFont:[UIFont systemFontOfSize:14.0]];
Also, look HERE for a detailed explanation of these functions which can also take into account the available width, minimum sizes, line breaks, etc.
The answer above mine is the best with the second part: use a UIView and put either your label or a UIImageView inside it depending on what you want. Here's what it would look like with the image:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(<<your image frame here>>)];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
image.frame = CGRectMake(<<your image frame here>>);
[container addSubview:image];
[self.view addSubview:container];

How to set the title of UIToolBar?

How can I set the title of UIToolBar such that it looks the same as the title in UINavigationBar?
I tried to use a button with plain style, it looks ok, but it will be highlighted when I click on it... Is there any better way to set the title in the detail view of split view?
This is what I use to present a title on a toolbar that will not highlight when pressed:
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
// choose whatever width you need instead of 600
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 600, 23)];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.shadowColor = UIColorFromRGB(0xe5e7eb);
label.shadowOffset = CGSizeMake(0, 1);
label.textColor = UIColorFromRGB(0x717880);
label.text = #"your title";
label.font = [UIFont boldSystemFontOfSize:20.0];
UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:label];
[label release];
I think this would be much cleaner:
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:frame];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:#"Your Title"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
NSArray *items = [[NSArray alloc] initWithObjects:spacer, item, spacer, nil];
[toolbar setItems:items];
toolbar.userInteractionEnabled = NO;
This solves the highlighting problem, disabled problem, and touch problem.
Toolbar and titleButton were both created in IB.
The view title is covered by the toolbar. So put the title in a toolbar.
self.titleButton.title = #"Order"; // myInterestingTitle
It looks like this:
Disable to prevent any highlighting, and to stop it from responding to touches.
self.titleButton.enabled=NO;
Then it looks like this:
It will look disabled, so set the color for disabled to white,
which has an implicit alpha=1.0.
This effectively overrides the 'disabled' look.
[self.titleButton setTitleTextAttributes:
[NSDictionary dictionaryWithObject:[UIColor whiteColor]
forKey:UITextAttributeTextColor]
forState:UIControlStateDisabled ];
Here's what you get:
Swift 5 version of Rayfleck's great answer:
let titleButton = UIBarButtonItem(title: "My Title", style: .plain, target: nil, action: nil)
titleButton.isEnabled = false
titleButton.setTitleTextAttributes([.foregroundColor : UIColor.black], for: .disabled)
Add this button to the toolbar as normal.
Put the regular "Rounded Rect Button" (UIButton) on toolbar (not "Bar Button Item")! Be aware, that behind the scene, IB wrap UIButton with UIBarButtonItem. It is why you have to click on button twice to get to UIButton properties (true for Xcode 4.2).
Get to UIButton properties. When you click first time, you will get "Bar button item" properties (it is what IB created automatically for you). Now click second time, you'll get to "Button" properties (also, when you selected UIButton you can't resize it and can't see resize markers).
In the Button properties set type to "Custom" to remove rounded rect border around button (you may need to resize button in IB to refresh it before you can see difference).
In the Button properties under "View" uncheck "User Interaction Enabled".
UILabel* title = [[[UILabel alloc] init] autorelease];
[title setBackgroundColor:[UIColor clearColor]];
[title setFont:[UIFont boldSystemFontOfSize:20]];
[title setTextAlignment:UITextAlignmentCenter];
[title setTextColor:[UIColor grayColor]];
[title.layer setShadowColor:[[UIColor colorWithWhite:1.0 alpha:0.5] CGColor]];
[title.layer setShadowOffset:CGSizeMake(0, 1)];
[title.layer setShadowRadius:0.0];
[title.layer setShadowOpacity:1.0];
[title.layer setMasksToBounds:NO];
[title setText:#"Sample Title"];
[title sizeToFit];
// [[[UIBarButtonItem alloc] initWithCustomView:title] autorelease]
Use a UIBarButtonItem in the plain style and additionally cover the toolbar in the appropriate area with a UIView that has a clear background. The view consumes the taps and hides them from the bar button item. Make sure you set the autoresizing mask correctly.
You can uncheck "Shows touch on Highlight" in Interface Builder.