changing the text attributes of nav bar - objective-c

i want to change the appearance of the nav bar, and so far i was able to change the background image of the nav bars, and also with the color.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
//set the bg image of all nav bars
UIImage *navBackgroundImage = [UIImage imageNamed:#"navigationBackground.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
return YES;
//customizing the title text of the nav bars
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:250.0/250.0 blue:240.0/240.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Heiti TC" size:21.0], UITextAttributeFont, nil]];
}
this is the code i used to achieve changing the nav bar bg image and the color. if you look into the 2nd UINavigationBar appearance statement, i try to set the font of the nav bar with
[UIFont fontWithName:#"Heiti TC" size:21.0]
but it wont change the font. btw i run this with iphone 6.1 simulator on xcode 4.6.2. im am sure that the font name is "Heiti TC".

Maybe it doesn't work because you
return YES;
before changing the font?

The name supplied for the method fontWithName is not the same as the displayed family name of the font.
Try "STHeitiTC-Light" or "STHeitiTC-Medium" instead.
[UIFont fontWithName:#"STHeitiTC-Medium" size:21.0]
And put the
return YES
at the end of the didFinishLaunchingWithOptions.
PS: The answer here is quite helpful to find the font name to be used: Cant find custom font - iOS

Everything looks right. I have used similar code to achieve it as expected. Have you tried using the font Heiti TC Light or Heiti TC Medium?

Related

MFMessageComposeViewController: Cannot Style "Cancel" Button in Dark Mode

I'm currently trying to style the "cancel" button that is in the top right corner (the navigation bar and the button are both grey) and I am having no luck. I've tried calling several different methods and none have worked. My project builds for iOS12.1 and I'm testing via iOS15.4. Here is what I've tried so far:
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Futura-Medium" size:17.0f],
NSFontAttributeName,
[UIColor blueColor],
NSForegroundColorAttributeName,
nil]];
MFMessageComposeViewController *messageController;
[messageController.navigationBar setTintColor:[UIColor blueColor]];
messageController = [MFMessageComposeViewController new];
[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[UINavigationBar class]]].tintColor = [UIColor RepublicBrightBlue];
The gray color for the button is likely coming from the global tint color for your application. This is by default AccentColor that comes within Assets (this is mapped via Build Settings). Note that you can define separate global accent colors for Any, Light, Dark
If you're just interested in styling this MFMessageComposeViewController the following worked for me to change the tint color for all views within the message view:
MFMessageComposeViewController *messageController = [MFMessageComposeViewController new];
[self presentViewController:messageController animated:true completion:nil];
[UIView appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[MFMessageComposeViewController class]]].tintColor = [UIColor redColor];
Which results in

iOS 7, unselected tint color?

I'm upgrading my app to meet iOS 7 guidelines. My main tab bar controller has a blue background and the default gray "unselected" tint makes the icons nearly invisible. How can I change the tint for these icons? I'd like them to be black or something that contrasts a lot with the blue background.
I've already configured an app-wide tint using window.TintColor = White, but that only changes the selected color. (Same with TabBar.TintColor)
I haven't tested it, but this answer looks like it's unfortunately the only solution
https://stackoverflow.com/a/18433996/1732987
Update: Copied code for posterity.
// set color of selected icons and text to red
self.tabBar.tintColor = [UIColor redColor];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], UITextAttributeTextColor, nil] forState:UIControlStateSelected];
// set color of unselected text to green
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], UITextAttributeTextColor, 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"];

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 :)

Changing Text Color of Unselected UITabBarItems

Here's what I've tried:
I've tried the following in the parent view controller:
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
I've tried the following in the application delegate:
[[UITabBarItem appearance]
setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil]
forState:UIControlStateNormal];
I've even tried changing UIControlStateNormal to any of the other available constants. The only one that changes anything is UIControlStateHighlighted which changes the color of the highlighted tab. Is this a bug in the API or is there something I'm missing?
Key things to note:
I'm using storyboarding
I have a UITabBarController where each tab has an embedded UINavigationController (pretty standard setup)
I've tried embedding the first code snippet into both the UINavigationController subclass as well as the root UIViewController subclass that is inside the UINavigationController. No luck there either.