changing tintColor makes backbutton disappear - objective-c

I am trying to modify navigationBar's appearence by setting it's tinkColor & barTintColor
start up with doc on page https://developer.apple.com/documentation/uikit/UINavigationBar?language=objc
I tried to modify the navbar on the [viewDidLoad] hook of my ViewController as follows
self.navigationController.navigationBar.translucent = NO;
UIColor *barColor = [UIColor ColorA];
self.navigationController.navigationBar.barTintColor = barColor;
UIColor *backButtonColor = [UIColor ColorB];
self.navigationController.navigationBar.tintColor = backButtonColor;
But then the back button then disappeared and the change of barTintColor dosen't seem to be effective
What am I doing wrong?

Weird behavior.
check please title of previous viewcontroller
check please if there is something like(hiding back button somewhere):
self.navigationItem.leftBarButtonItems = []
self.navigationItem.hidesBackButton = true
double check color for backButtonColor(be sure that barColor != backButtonColor). Try some native color: [UIColor red]

Related

UIPageViewController: Change color of page indicator/pagecontrol per page

I am able to successfully change the color of PageControl/page indicator on UIPageViewController for the first page only. My use case requires each page to be a different color, requiring the PageControl/page indicator to match color with a page. I tried changing color based on the page index but it doesn't work.
Anyone know how to fix this?
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if ([tutorialItems count] == 0) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController
= [self.storyboard instantiateViewControllerWithIdentifier:#"PageContentViewController"];
TutorialItem *tutorialItem = [tutorialItems objectAtIndex:index];
pageContentViewController.imageFile = tutorialItem.tutorialImageFileName;
pageContentViewController.titleText = tutorialItem.tutorialTitle;
pageContentViewController.tutorialText = tutorialItem.tutorialText;
int red = tutorialItem.tutorialRedKey;
int green = tutorialItem.tutorialGreenKey;
int blue = tutorialItem.tutorialBlueKey;
pageContentViewController.pageIndex = index;
tutorialBackgroundColor = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1.0f];
pageContentViewController.tutorialBackgroundColor = tutorialBackgroundColor;
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor blackColor];
pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
pageControl.backgroundColor = [UIColor clearColor];
pageContentViewController.pageControllerColor = tutorialBackgroundColor;
return pageContentViewController;
}
appearance proxy attributes only get set when a component is first added to the window. Changing an appearance attribute will not change it for already visible components. If you want to change the color of a component that is already on the screen, you will need to reference it and change the color on it directly, or remove it from the view hierarchy and re-add it.
You can read the documentation here
Relevant quote:
iOS applies appearance changes when a view enters a window, it doesn’t
change the appearance of a view that’s already in a window. To change
the appearance of a view that’s currently in a window, remove the view
from the view hierarchy and then put it back.

Navigation bar buttons lost tint on iOS 7

I am experiencing strage issue on iOS7. The navbar just after first time user gets to main screen:
The navbar after simple push/pop
The setup in storyboard:
It's setup for logout button, the same is set up for search button.
There's no dealings performed with navigationItem in code. Any suggestions?
EDIT For sure i can do the trick with
- (void)viewWillAppear:(BOOL)animated
{
self.leftBarButtonItem.tintColor = [UIColor whiteColor];
self.rightBarButtonItem.tintColor = [UIColor whiteColor];
}
But i don't understand the reason of losing tint. And this is the question, not what to do.
Since iOS7 you need to set the tintColor for the navigationBar to change the buttonItems color:
self.navigationBar.tintColor = [UIColor whiteColor];
Or you can set it from the storyboard as well.
For more explanation check this SO answer.

UINavigationBar editable title?

I have a UINavigationController based app. I can programatically set the title of the UINavigationBar from within my view controller's viewDidLoad method:
self.navigationItem.title = m_name;
Can I make it so that the title in the navigation bar is editable? IE. I want the user to be able to tap the title in the navigation bar, and to be able to edit it.
Is this possible? And if it is possible, does is it likely to meet Apple's Human Interface Guidelines? (This post suggests it will, but does not tell me how to implement it - Make UINavigationBar title editable)
Many thanks
Nathan
You are able to set a custom view for your title.
titleView
A custom view displayed in the center of the navigation bar when the
receiver is the top item.
#property(nonatomic, retain) UIView *titleView
Discussion
If this property value is nil, the navigation item’s title is
displayed in the center of the navigation bar when the receiver is the
top item. If you set this property to a custom title, it is displayed
instead of the title. This property is ignored if leftBarButtonItem is
not nil.
Custom views can contain buttons. Use the buttonWithType: method in
UIButton class to add buttons to your custom view in the style of the
navigation bar. Custom title views are centered on the navigation bar
and may be resized to fit.
If you were to place a UITextField in your titleView you could give it a clearColor background and set it to have no border. Wala you have a UINavigationBar Title you can tap and edit.
Ryan's solution is exactly right. In case anyone is having trouble implementing it, here is some sample code that should get you going. Just paste it into ViewDidLoad on any class with a navigationController.
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 22)];
textField.text = #"Insert Title Here";
textField.font = [UIFont boldSystemFontOfSize:19];
textField.textColor = [UIColor whiteColor];
textField.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = textField;
And if your project doesn't use ARC, make sure to release the textField like so:
UITextField *textField = [[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 22)]autorelease];
or like so:
[textField release];
Hope that helps.
Ryan's solution for swift
let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 22))
textField.text = "Title"
textField.font = UIFont.systemFontOfSize(19)
textField.textColor = UIColor.whiteColor()
textField.textAlignment = .Center
self.navigationItem.titleView = textField
Here is Ciprian's code in Swift 4 and corrected for iOS 12:
let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
textField.text = "Your Title"
textField.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
textField.textAlignment = .center
self.navigationItem.titleView = textField
You can put a text field right ON TOP the navigation bar, and set it hidden/not enabled., the use a gesture recognizer to detect a tap on the navigation bar and set the text field to enabled/ not hidden. Then in the textfield's did end editing method, set it to hidden again and set navigation bar's title to that of the textfield.
Add a hidden textfield over navigation bar, so on tapping, it will popup a keyboard and user can enter text into that.
Get the return button event, over there you can change your navigation bar text with newly added text. And hide the textfield again.
Enjoy Coding :)
Put a textField on the NavigationBar and set the textField background color with opacity to zero.

Objective C: How to change text color in navigation bar

I have changed my navigation bar color via the following code
navconFvc.navigationBar.tintColor = [UIColor colorWithHexString:#"faf6f5"];
The code worked but the text color also needs to be changed (see screenshot below). Also the refresh button logo on the right is affected as well
The same issue occurs if I navigate to another page in the stack
Question: How can I change the color of the
title text
Back button text and
right bar button icon color?
After I changed the background color of the navbar?
In iOS 7, just use:
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor whiteColor]};
Change [UIColor whiteColor] with whatever text color you want
For the title here's the way:
iPhone Navigation Bar Title text color
And for the custom buttons here's the way:
adding buttons to ui navigation controller bottom bar
To change text color:
_navController.navigationBar.titleTextAttributes
= #{UITextAttributeTextColor : [UIColor blackColor]};
Adding refresh button and color it:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:#selector(reload)];
[button setTintColor:[UIColor blackColor]];
self.navigationItem.rightBarButtonItem = button;
Variables that effect navigation bar background:
_navController.navigationBar.backgroundColor = [UIColor whiteColor];
_navController.navigationBar.tintColor = [UIColor whiteColor];
_navController.navigationBar.translucent = NO;
I just put together a simple UIViewController subclass that adds a customizable back button that allows you to change text colors. It basically adds some willAppear/willDisappear logic to animate the back button the way the UINavigationController does while using the leftBarButtonItem property. You might extend this to also do the rightBarButtomItem as well.
https://github.com/typeoneerror/BBCustomBackButtonViewController

Maintain UITableViewCell background color when going into edit mode

I have set background colors for all my UITableViewCells. However, when I click my UIBarButtonItem "edit", the delete and the draggable icons distort the background color, making white background behind them. Is there any way around this? I can show code if necessary, but this seems like a pretty straightforward question.
The background color of a table cell is usually set like this:
cell.backgroundView.backgroundColor = [UIColor redColor];
This works fine. However, when an object of class UITableViewCell is created, by default it does not have a backgroundView, it is nil. The developer needs to create the backgroundView when needed. So depending on the particular situation you need to do something like:
if (cell.backgroundView == nil) {
cell.backgroundView = [[[UIView alloc] init] autorelease];
}
cell.backgroundView.backgroundColor = [UIColor redColor];
This is the default behavior of the uitableviewcells in the editing mode. Try setting the background color for your tableViewCells again in
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
if(editing)
{
// set background color
}
else {
}
If needed, try setting your background color as your property so that you can set it up here.