Change separator color in UISegmentController? - ios

My project have UISegmentController. I have gray color separator.
in between the black line is the separator, there is white separator, but I want gray separator ? Please suggest some code.
My code is
- (void)changeColor{
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithRed:83.0f/255.0f green:198.0f/255.0f blue:255.0f/255.0f alpha:1.0]} forState:UIControlStateSelected];
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithRed:197.0f/255.0f green:197.0f/255.0f blue:197.0f/255.0f alpha:1.0]} forState:UIControlStateNormal];
}

You have to set it using an image. Note that you can have separate dividers between a selected and unselected segment or between two unselected segments.
UISegmentedControl.setDividerImage(_:forLeftSegmentState:rightSegmentState:barMetrics:)
Intead of using a UISegmentedControl I find it simpler to use a set of UIControl elements.

Related

Why my tabbar was rendered with gray rect?

// Tabbar
// Icon inactive
[[UITabBarItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateNormal];
// Icon active
[[UITabBar appearance] setTintColor:PRIMARY_COLOR];
// Text active
[[UITabBarItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: PRIMARY_COLOR} forState:UIControlStateSelected];
// Background
[[UITabBar appearance] setBarTintColor:WHITE_COLOR];
// Over View YES/NO
[[UITabBar appearance] setTranslucent:NO];
I've a gray rect around the text and the icon.
Why? Where my damn mistake?
After discussion, we find that #CeccoCQ used another setting for UIView. That makes every view have a custom background color (in the image, it's gray color).
[[UIView appearance] setBackgroundColor:WINDOW_BACKGROUND_COLOR];
To resolve the problem, remove this custom setting and everything will work fine.

How to set a different unselected image and text color on UITabBarItem

I'd like to set different colors for a UITabBarItem's title text and image in the unselected state.
For the selected state, I can accomplish this like so:
[[UITabBar appearance] setTintColor:[UIColor purpleColor]]; // image color
[[UITabBarItem appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName: [UIColor orangeColor] } forState:UIControlStateSelected]; // text color
For the unselected state, I'm attempting the following:
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blueColor]]; // image color
[[UITabBarItem appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName: [UIColor redColor] } forState:UIControlStateNormal]; // text color
But for some reason, the unselectedItemTintColor setting overrides whatever I try and set for the titleTextAttributes – so in the snippet above, both the text and image would appear blue.
I've also tried changing the titleTextAttributes directly on the UITabBarItem after I've created it (instead of using appearance), but again this seems to have no effect.
How can I achieve different unselected colors? Is it possible?
I managed to solve this shortly after posting. It turns out that while setting the unselectedItemTintColor using UIAppearance overrides the titleTextAttributes for the item, everything works correctly if you set the unselectedItemTintColor directly on the tab bar itself.
So instead of
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blueColor]];
just do
[self.myTabBarInstance setUnselectedItemTintColor:[UIColor blueColor]];

In a UISegmentedControl, how do I set the normal color separate from the selected color?

This is the effect that I'm going for:
This is what I have so far:
Cocoa does not allow you to set both an icon and text for each segment, so I was forced to burn the text into an image:
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:#[
[UIImage imageNamed:#"segmentedControlContacts"],
[UIImage imageNamed:#"segmentedControlOtherApps"]
]];
The last thing I have to finish is making the normal (unselected) segment gray instead of its selected/tint blue color. The following did not work:
// themeColor is defined as a shade of blue in a category
[UIView appearance].tintColor = [UIColor themeColor];
[[UISegmentedControl appearance]
setBackgroundImage:[UIImage imageNamed:#"segmentedControlEdgeNormal"]
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance]
setBackgroundImage:[UIImage imageNamed:#"segmentedControlEdgeSelected"]
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance]
setTitleTextAttributes:#{
NSForegroundColorAttributeName: [UIColor grayColor]
}
forState:UIControlStateNormal
];
The line
[[UISegmentedControl appearance] setTitleTextAttributes:#{
NSForegroundColorAttributeName: [UIColor grayColor]
}
forState:UIControlStateNormal
];
Will only works for title of segment control. If you remove images from segment and add title for both segment than this will surely works.
This will not take an effect on images for segment control.
For image customisation you must have to use method:
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
Also it will not show tint colour as you expected.
Suggestion:
If you still insist to make that custom behaviour, I suggest leave segment control instead create two buttons and place them side by side to feel like a segment control.
And for that behaviour you need grey image for button background and change for alternate button events.

How to set UISwitch border color?

My app has:
[[UIView appearance] setTintColor:[UIColor whiteColor]];
And here is what I have when on:
and off:
I need to make UISwitch border visible like in Settings.app:
Your [[UIView appearance] setTintColor:[UIColor whiteColor]]; is interfering with the tint color of your switch. The command to set the tint color is self.mySwitch.tintColor = [UIColor grayColor]; which sets the color used to tint the outline of the switch when it is turned off.
Will you please try adding this line to your AppDelegate's didFinishLaunchingWithOptions
[[UISwitch appearance] setTintColor:[UIColor grayColor]];
This should apply the chosen Tint color on all your UISwitch controls.
Rather than using the appearance proxies you can also use:
[self.mySwitch setThumbTintColor:[UIColor blueColor]];
[self.mySwitch setOnTintColor:[UIColor redColor]];
ie. Use setOnTintColor for the background/border color.

IOS 7 UISegmentControl set tint color not working

IOS 7 UISegmentControl set tint color not working.
How am I going to solve it?
Here is my code for adding:
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:[NSMutableArray arrayWithObjects:#"YES",#"NO", nil]];
[segmentControl setTintColor:[UIColor orangeColor]];
but the color only appear at border, i want it to color whole background.
As I referred from the Library,
#property(nonatomic) UISegmentedControlStyle segmentedControlStyle NS_DEPRECATED_IOS(2_0, 7_0, "The segmentedControlStyle property no longer has any effect");*
The UISegmentedControl relates to the tint color of the UINavigationBar.
If you change the color, the tint color of the UISegmentedControl will change accordingly.
[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
I think you may want:
[segmentControl setBackgroundColor:[UIColor orangeColor]];

Resources