I am globally setting the appearance for all my UISegmentedControl's. However I noticed that when I do this it breaks the disabled state.
Here is the code I have to change the titleTextAttributes
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:12], UITextAttributeFont,
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor colorWithRed:0x49/255.0 green:0x49/255.0 blue:0x49/255.0 alpha:1], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
nil];
[[UISegmentedControl appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
Now this works great and all segmented controllers are updated.
But the items that I had disabled with setEnabled:forSegmentAtIndex: aren't disabled anymore and are clickable. Why does this happen? How can I get them to be disabled again. I tried to apply it to the disabled state as well but it doesn't work.
[[UISegmentedControl appearance] setTitleTextAttributes:attributes forState:UIControlStateDisabled];
Well it appears that setting a global appearance for the UISegmentedControl wasn't the best approach as it does not honor the UIControlStateDisabled.
I thought I had to use UIControlStateDisabled since nothing was working, but it appears that setting the background directly works. So I just ended up making a UISegmentedControlExtended class that initializes the background and now the setEnabled function works fine.
Related
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
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?
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.
I'm able to change the text color of UIBarButtonItem using this code snippet (iOS 5+):
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[UIColor blackColor] forKey:UITextAttributeTextColor];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
Unfortunately this does not affect UIBarButtonSystemItemAdd.
Is there a way to change the text color of the UIBarButtonSystemItemAdd ?
Use setTintColor. For example:
[downloadButtonItem setTintColor:[UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1.0]];
I know no way to achieve this -- system UIBarButtonItem instances use images instead of text, so changing their color cannot be done by changing their title text color. You most likely have to create your own bar button item.
I got a program from a colleague and he is using .xib files to setup his view.
(being a hard core coder, I never used IB myself...)
So he is setting up his ViewControler and he has a NavBar (that I could not find in the .xib file!).
I want to add drop shadows to the title.
If I was making the NavBar, the relative code would be something like:
textViewTitle.layer.shadowOpacity = 2.0;
textViewTitle.layer.shadowRadius = 3.0;
textViewTitle.layer.shadowOffset = CGSizeMake(2.0, 3.0);
and that works fine.
Digging around I found that now I need to use this:
NSDictionary *navbarTtlAts = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(2.0, 3.0)], UITextAttributeTextShadowOffset,
nil];
[self.navigationController.navigationBar setTitleTextAttributes:navbarTtlAts];
Although this works, I could not find how to add the shadowOpacity & shadowRadius stuff.
I found that I could add the following in the NSDictionary, but it did not work...
[NSNumber numberWithFloat: 2.0], #"shadowOpacity",
[NSNumber numberWithFloat: 3.0], #"shadowRadius",
Any ideas?
You should use these keys for the shadow color and offset.
UITextAttributeTextShadowColor
UITextAttributeTextShadowOffset
There is no way to specify the shadow radius as a text attribute.
If you really wanted a custom shadow you could create your own UILabel and set the shadow like in your first example. Then you would set that label as the titleView of your view controllers UINavigationItem.
You would have to update the text yourself though (the navigation controller would no longer do it for you automatically).