UINavigationBar setTitleTextAttributes - objective-c

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

Related

Adding two buttons to rightBarButtonItems not working

I am not sure why the following code is not working:
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:#"New" style:UIBarButtonItemStyleBordered target:self action:#selector(newClicked:)];
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(share:)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:newButton, shareButton, nil];
It only display the "newButton" UIBarButtonItem on the navigation bar and not the "shareButton" button.
I found a different issue involving this behavior.
If you've set a custom titleView (navigationItem.titleView = ...) then the navigation item can sometimes hide your buttons except the first one if it doesn't have room.
So make sure you limit the width of your custom titleView.
rightBarButtonItems is in ios 5.
I think the issue is with the objet name new, please change this to any other name like newButton or something like that.
Because new is a keyword used for memory allocation in C++
Looks like the problem had to do with code following the questioned code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"Marker Felt" size:26.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;
self.navigationItem.titleView = label;
[label release];
Posting this now in case someone else encounters this problem!

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];

Set the title of the UINavigationBar

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";

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.

How to customize the background color of the standard label within a UITableViewCell?

I'm trying for the past several hours to figure out how to do this, but with no luck. There are several potential solutions for this when searching Google, but nothing seems to work.
I'm trying to customize the background color of the standard UILabel that goes in a UITableViewCell (since I already customized the background color of the cell itself), but nothing I do seems to work.
I'm creating my own UILabel to customize the colors in -tableView:cellForRowAtIndexPath:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor blueColor];
label.backgroundColor = [UIColor redColor];
label.opaque = YES;
[cell.contentView addSubview:label];
cell.text = #"Sample text here";
But that doesn't work, and the resulting table view still has a bunch of cells with labels with black text and white background in it.
Any clues on what I am doing wrong here?
UPDATE: If I try to do the following instead:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor blueColor];
label.backgroundColor = [UIColor redColor];
label.opaque = YES;
[cell.contentView addSubview:label];
label.text = #"Sample text here";
I get a bunch of UITableViewCells with no text at all.
It appears that you're assigning the text to the cell instead of the label. You probably want:
label.text = #"Sample text here";
Also, you'll need to set the label's frame to what you require:
label.frame = CGRectMake(10,10, 80, 40);
or directly in the constructor:
label = [[UILabel alloc] initWithFrame:myFrame];
I wouldn't do this in code. I would do it with a custom XIB file and then load it in your
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
delegate function. That way you can design the XIB file to your exact specs, and tweak it.
Good luck!
You have asked this question before, and received correct answers;
How to customize the background color of a UITableViewCell?