UISegmented Control Global Tint in Navigation Controller Header - ios7

Setting the global tint in my tableview header isn't tinting as expected, and appears to be a bug.
When I load the view I get
The right hand side retains the default colour, however if I press the right button and the left again it sets to how you would expect it to be:
This problem is only with the segmented control in my navigation bar. The global tinting works when the control is embedded in a "normal" view.
I have a work around which solves the problem. Being, I setting the right control and then back to the left in viewDidLoad.
So is it a bug ? or am i missing something? Thanks.
Add: I am setting the global tint in the storyboard

It's not a bug. Global tinting works for the objects, whose class is used for tinting. If the specific tint scheme is declared for a class, then all the controls belonging to that class will follow the class tint scheme regardless of the view they are added to.
I have tried implementing this & I am here with this code.
Set the global Tinting for UINavigationBar in appDelegate
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
...
...
}
and I am adding UISegmentControl in UINavigationBar as well as intableView.tableHeaderView in viewDidLoad method.
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:#[#"One", #"Two"]];
segment.frame = CGRectMake(self.view.frame.size.width/2-75, 0, 150, 40);
[self.navigationController.navigationBar addSubview:segment];
UISegmentedControl *tableSegment = [[UISegmentedControl alloc]initWithItems:#[#"One", #"Two"]];
tableSegment.frame = CGRectMake(self.view.frame.size.width/2-75, 0, 150, 40);
[self.navigationController.navigationBar addSubview:tableSegment];
tableView.tableHeaderView = tableSegment;
This yields following result. The UISegmentControl will be tinted with the UINavigationBar 's global tint if it is added into the UINavigationBar container as SubView, otherwise, it will take the default blue color outside UINavigationBar like in the header of UITableView
Now adding a specific tint color for UISegmentedControl as
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
[[UISegmentedControl appearance] setTintColor:[UIColor brownColor]];
...
...
}
``
It yields this, The UISegmentControl is affected by its global tint now, regardless of its location (like earlier in UINavigationBar & UITableViewHeader). It is
However the TinitColor of UInavigationBar is still applicable on the leftBarButtonItem because there is no global tint scheme declared for the UIBarButtonItem, hence it is adopting the tintColor of the container it is added to.
Adding more code to Clarify
Using global tint for UISegmentedControl & no other global tinting.
[[UISegmentedControl appearance] setTintColor:[UIColor colorWithRed:39.0/255.0 green:64.0/255.0 blue:112.0/255.0 alpha:1]];
Result: You can see the segment control, but notice the backButton, which is having the default tint of UINavigationBar
Using global tint for UISegmentedControl & UINavigationBar
[[UISegmentedControl appearance] setTintColor:[UIColor colorWithRed:39.0/255.0 green:64.0/255.0 blue:112.0/255.0 alpha:1]];
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:39.0/255.0 green:64.0/255.0 blue:112.0/255.0 alpha:1]];
Result: You can see the segment control tint & also notice the backButton, which is having the Global tint of UINavigationBar
I have also done a default selection of UISegmentedControl. If you are getting any other result with this code then there must be some thing extra happening in your code.
I hope that helps.

I've had a big issue with tinting in Xcode 5.0.2 recently. I was playing with both Global Tint and Tint for various items when suddenly the inheritance of tintColor stopped working correctly. I ended up cleaning the Storyboard's XML file manually to fix things out.
If you're not doing any UIAppearance magic in your app, it sure looks like a bug to me.

Related

Customizing UINavBar with custom color and without hairline

I want to set my navigation bar to the same color as my UITableViewCell(s), while also hiding the pixel line between the navigation bar and the cells.
The code I've used, in the AppDelegate didFinishLaunchingWithOptions, is this:
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:0.465639 green:0.763392 blue:1 alpha:1]];
It removes the pixel line and sets the Nav Bar's color.
However, it leaves the status bar as transparent and my table view appears in the status bar when I scroll down.
How can I add the color to the status bar as well?
Thanks!
If its a solid colour you could always use:
self.navigationController.navigationBar.translucent = NO;
To stop the content going behind the bar
And use the new barTint property
self.navigationController.navigationBar.barTintColor = [UIColor redColor];

UINavigationBar color has changed after upgrading to latest Xcode

Recently I've updated my Xcode IDE to the latest to have iOS 7.1 SDK. For some reason, after recompiling my app with the new Xcode I get this wired UINavigationBar background colour.
I'm using storyboard to push the secondary view controller from home screen. The secondary view controller is a static table of settings, also built using storyboard, and contains the following code in its viewDidLoad method:
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = nil;
I haven't done any other changes to this screen or any other screens in the app.
This is how it used to look like before:
This is how it is look like now:
Does anyone have any idea what could have caused this?
Cheers,
EDIT: #Leo Natan -
When I'm trying to set the color myself, for example, by replacing this line:
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
with this line:
[self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];
I get this: the red color applied only on the header without the status bar, what can I do?
Latest Xcode comes with iOS7.1 SDK, in which Apple once again changed how the color is calculated. You will have to play again with your color to achieve a similar look in iOS7.1.
Maybe you should take another approach. Use the Appearance Proxy in the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions. Thats how I do it:
//Navigationbar
[[UINavigationBar appearance] setBarTintColor:lightOrangeColor];
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"navbarAssets-backArrow-iOS7"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"navbarAssets-backArrow-iOS7"]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes: #{NSForegroundColorAttributeName : [UIColor whiteColor]}];

How can I make my navigation bar uniformly semi-transparent?

I'm writing an app on iOS 7, and I can't seem to get a handle on the transparency of the navigationBar and the toolbar, how can I set the navigation bar to black at 50% opacity?
I've read the transition to ios7 guide and I've watched the wwdc13 lecture 214, but my status bar still has a different transparency than the rest of the attached nav bar.
Here is my code:
// APP-WIDE THEMING
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackOpaque];
[[UINavigationBar appearance] setBackgroundColor:[UIColor blueColor]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Here is the screenshot of my problem: http://grab.by/qiyU
Set the background image to nil, and set the background color with alpha.
[ctrl.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
ctrl.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:0 Green:0 Blue:0 Alpha:.5];
According to the answer posted here it's possible to create a transparent UINavigationBar:
How to draw a transparent UIToolbar or UINavigationBar in iOS7
However you want to create a semitransparent navigationbar. For that as far as I can tell you have to create a 1 px large image containing a black color with 50% opacity. Add this as backgroundimage for your Navigationbar.
This snippet should do the trick:
[[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
UIImage* sti = [UIImage imageNamed:#"EMT_SemiTransparent.png"];
[[UINavigationBar appearance] setBackgroundImage:sti forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:UIColor.clearColor];
try setting key [View controller-based status bar appearance] to NO in your pList file as well.
i've run into some funkiness with the status bar not seeming to be affected by changes in code and this solved it for me.
reference: https://stackoverflow.com/a/18184831/2962193
Set the alpha value to make it transparent.
[[UINavigationBar appearance] setAlpha:0.5f];

how to run code in tabbarcontroller

Im trying to change the icon tint of my unselected tab bar icon images. I have used the patch code below, however, the post that i found this patch code in says to run this in the tab bar controller but i did not know how to do that so i ran it in the -(void)viewDidLoad method in the viewcontroller .m file. It came up with an error saying "Property 'tabBar' not found on object of type 'ViewController *'" How do i fix this?
// set color of selected icons and text to red
self.tabBar.tintColor = [UIColor redColor];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
// set color of unselected text to green
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil]
forState:UIControlStateNormal];
// set selected and unselected icons
UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
// this way, the icon gets rendered as it is (thus, it needs to be green in this example)
item0.image = [[UIImage imageNamed:#"unselected-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// this icon is used for selected tab and it will get tinted as defined in self.tabBar.tintColor
item0.selectedImage = [UIImage imageNamed:#"selected-icon.png"];
Instead of using that patch of code, try to do the below in your VC:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"item_unselected.png"]];
Edit:
The code above is used when subclassing UITabBarController, so if you insis you need to subclass the 'UITabBarController', change the class of your tabBarController in the storyboard and put that block of code in its viewDidLoad.
You can delete your UIViewController from storyboard and add there UITabBarController and set it as Initial View Controller.
Then you can access this UITabBarController in AppDelegate.m like this:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
This is the fast way.
Also you can subclass UITabBarController, set this class in the storyboard and put all your code inside this subclass. (as null has said 15 minutes before :)

how to change the color of the tabbar controller

Is there any way I can change the Tab Bar Controller's color to something other than the default black? I know this isn't possible in IB, but perhaps maybe through code?
In AppDelegate
self.tabBarController.moreNavigationController.navigationBar.tintColor = [UIColor grayColor];
In AppDelegate.m in didFinishLaunching... method write(this will change for whole app):
[[UITabBar appearance] setBarTintColor:[UIColor myColor]];
Or you can write in ViewController.m in method viewDidLoad:
[self.tabBarController.tabBar setBarTintColor: [UIColor mycolor]];
You can do with XIB/Storyboard as well as programmatically
For Xib/storyboard select tab bar controller than tab and you can see all the options to change tab bar or tab bar view properties see the attached image Image attached here
For programmatically:
for tab bar tint and background
[[UITabBar appearance] setTintColor:panelColor];
[[UITabBar appearance] setBarTintColor:[UIColor lightGrayColor]];