UIBarButtonItem tintColor stopped working - ios

For some reason I can't use UINavigationBar on my view and I use UIToolBar instead. I was using dummy left button with clear tintColor and title button with black tintColor to properly center the title and hide the dummy left bar button, but it's not working anymore and both the dummy left button and the title button get either the main tintColor or if I disable them, they become gray.
How can I make them ideally disabled as buttons and set the proper tintColor (clear and black)?
- (void)addToolbar
{
CGFloat barHeight = 44.0;
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.width - barHeight, self.view.bounds.size.height, barHeight)];
UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
// Done button on the right
UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
// dummy button on the left to have centered title
UIBarButtonItem *dummyButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
dummyButtonItem.enabled = NO;
dummyButtonItem.tintColor = [UIColor clearColor];
// title button
UIBarButtonItem *titleButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Title text here" style:UIBarButtonItemStylePlain target:nil action:nil];
titleButtonItem.enabled = NO;
titleButtonItem.tintColor = [UIColor blackColor];
// set toolbar items
toolbar.items = #[dummyButtonItem,flexibleSpaceButtonItem, titleButtonItem, flexibleSpaceButtonItem,doneButtonItem];
[self.view addSubview:toolbar];
}

When you specify a tint for a view, the tint is automatically propagated to all subviews in the view’s hierarchy. Because UIWindow inherits from UIView, you can specify a tint color for the entire app by setting the window’s tint property using code like this:
Under application:didFinishLaunchingWithOptions:
window.tintColor = [UIColor purpleColor];
Source - iOS Transitions . Check it out under Using Tint Color section

Based on your feedback I found that by doing this in AppDelegate:
self.window.tintColor = [UIColor colorWithRed:255/255.0 green:59/255.0 blue:48/255.0 alpha:1.0];
[[UITextField appearance] setTintColor:[UIColor colorWithRed:66/255.0 green:107/255.0 blue:242/255.0 alpha:1.0]];
the second line causes my issue. I now need to find out why did I put the second line there :). Thanks for your help.

Related

How to make color for UIToolbar the same as keyboard

I have issue with UIToolbar background color above the keyboard, here the picture: not expected background color on UIToolbar. I expected to see the same color as keyboard background, what I'm doing wrong?
I have this code:
UIToolbar* toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];
UIBarButtonItem *spaceBarButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil]; // to make close button on right side
UIBarButtonItem *closeBarButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemClose
target:self action:#selector(endEditing:)];
toolbar.items = #[spaceBarButton, closeBarButton];
textField.inputAccessoryView = toolbar;
So It seems I found solution its kinda hack, but still working.
UIInputView on init have inputViewStyle which mimics the keyboard background. If use UIInputView as parent view for UIToolbar with a clear background color, we will be good. Or use UIInputView itself, and add buttons on it, and apply it on inputAccessoryView without UIToolbar.
It depends on your content and future needs.
Example of keyboard what will shows: toolbar with right color implemented
My example with UIToolbar as subclass you can implement variant you want:
//add toolbar
UIToolbar* toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];//get height for toolbar
CGFloat width = [UIScreen mainScreen].bounds.size.width;//set width for toolbar
CGRect frame = toolbar.frame;
frame.size.width = width;
toolbar.frame = frame;
//toolbar buttons setting
UIBarButtonItem *spaceBarButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil]; // to make close button on right side
UIBarButtonItem *closeBarButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemClose
target:textField action:#selector(endEditing:)];
toolbar.items = #[spaceBarButton, closeBarButton];
//make toolbar background clear color
[toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
[toolbar setBackgroundColor:[UIColor clearColor]];
//creating UIInputView for using its background color
UIInputView *inputView = [[UIInputView alloc] initWithFrame:toolbar.bounds inputViewStyle:UIInputViewStyleKeyboard];
// UIInputViewStyleKeyboard - view mimics the keyboard background
[inputView addSubview:toolbar];
textField.inputAccessoryView = inputView;

How do I hide that line underneath the UINavBar?

This is the code I am using in viewWillAppear:
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 1420, 50)];
//do something like background color, title, etc you self
[[UINavigationBar appearance] setBarTintColor:[UIColor underPageBackgroundColor]];
self.navigationController.navigationBar.translucent = NO;
[self.view addSubview:navbar];
UINavigationItem *item = [[UINavigationItem alloc]
init];
navbar.items= #[item];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:self
action:#selector(backBtnClicked:)];
item.leftBarButtonItem = backButton;
I want to remove the shadowImage. The line underneath the nav bar but ONLY in one view controller. How do I do this?
I've already tried this but it changes all view controllers, to be like, invisible:
[[UINavigationBar appearance] setBackgroundImage: [UIImage new]
forBarMetrics: UIBarMetricsDefault];
[UINavigationBar appearance].shadowImage = [UIImage new];
Any help would be greatly appreciated!
I had an issue with white space appearing under my nav bar. Is this what you mean by a line? I solved it within xCode's Interface Builder by:
Selecting the view controller in the storyboard
Going to the attributes section of the right toolbar
Check the box "Under Opaque Bars" in the "Extend Edges" section.
I also added the following code in my viewDidLoad method:
self.automaticallyAdjustsScrollViewInsets = NO;

UICollectionView Top Bar Doesn't Appear In Simulator

I had a little problem which is my uicollectionviewcontroller top bar doesn't appear in simulator and device. I already set the top bar to translucent navigation bar for uicollectionviewcontroller, then I dragged navigation item to navigation bar and finally I set an image for left bar button items to function as back button. Below is there screenshot.
Top Bar Storyboard:
Simulator Result:
It doesn't look like you have a navigation controller, to add a navigation bar programatically without a navigation controller controlling the view hierarchy you can do the following.
-(void) viewWillAppear:(BOOL)animated {
UINavigationBar *navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[UINavigationBar appearance].barTintColor = [UIColor lightGrayColor];
[self.view addSubview: navBar];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self action:nil];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Navigation Title"];
navItem.rightBarButtonItem = doneItem;
navItem.leftBarButtonItem = cancelItem;
navBar.items = [NSArray arrayWithObjects: navItem,nil];
[UIBarButtonItem appearance].tintColor = [UIColor blueColor];
}
To add the navigation controller (which will add the bar), highlight the UICollectionViewController in storyboard, and select Editor->Embed in->Navigation Controller from the menus.

controller navigationItem.leftBarButtonItem changes color in IOS7

I have an image that is assigned to the navigationItem.leftBarButtonItem on IOS7. When I get it from the designer it looks fine on the navigation bar's background. But when I implement it in IOS7 it becomes pale and almost disappears.
This is how it's being set up:
UIBarButtonItem *button = [[barButtonItemClass alloc] initWithImage:[UIImage imageNamed:#"nav_menu_icon.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(showLeft:)];
viewController.navigationItem.leftBarButtonItem = button;
This is how it should look (image came from my designer):
And this is how it looks when implemented (on the simulator or a phone):
What's the solution?
You should set the tint color of the navigation bar to tint all items in the bar.
A nice solution is to use the UIAppearanceprotocol introduced in iOS 5.
In you AppDelegate's applicationDidFinishLaunching method put the following code:
[[UINavigationBar appearance] setTintColor: [UIColor whiteColor]];
Note, I am currently not able to try the code.
Cheers!
this works fine for me :
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStyleBordered target:self action:#selector(backBtnAction)];
customItem.imageInsets = UIEdgeInsetsMake(0.0, -10, 0, 0);// For adjusting the image
[customItem setBackgroundVerticalPositionAdjustment:+3.0f forBarMetrics:UIBarMetricsDefault];
[self.navigationItem setHidesBackButton:YES];// Hide the Default back button before set custom
[self.navigationItem setLeftBarButtonItem: customItem];
Hopefully, causes might be with the Tint
Try this, may help you ;
// Solve your frame issue.
UIImage * bgImg = [UIImage imageNamed:#"nav_menu_icon.png"];
UIButton *leftButton = [[UIButton alloc] initWithFrame: CGRectMake(x, y, bgImg.size.width, bgImg.size.width)];
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
Also change the tintColour to clear colour.

How do I force UIBarButtonItem text to a specific color?

I have a UIBarButtonItem on a navigation bar. When I enter my app, it starts out white (the desired color) and then switches abruptly to a faded out gray color. If I interact with the app and later return to the same view controller, the button returns to the white color. What could be causing this?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonItemStyleDone target:self action:#selector(handleBack:)];
[backButton setTitleTextAttributes:[NSDictionary dictionaryWithObject: [UIColor whiteColor]
forKey: UITextAttributeTextColor] forState:UIControlStateNormal];
//[backButton setTintColor: [UIColor whiteColor]];
self.navigationItem.leftBarButtonItem = backButton;
[self.navigationController.navigationBar setBarStyle: UIBarStyleBlack];
[self populateSpinners];
isCategoryPicker = true;
[self.view setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];
}
I've tried setting the tint color at the view level, the toolbar level, and at the button level and nothing seems to be working. Any help is appreciated!

Resources