Adding image to the segment controller - ios7

Prior to iOS 7 it was easy to add colored image to the segment controller, but now if I add image to it, only the image is visible with the default Tint color and not the actual color.
Does any one have idea about this, if so kindly help me out.
Thanx in advance.

Fix it with: imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
UIImage *segmentImage = [UIImage imageNamed:#"Example"];
if ([UIImage instancesRespondToSelector:#selector(imageWithRenderingMode:)]) {
segmentImage = [segmentImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
[self.control insertSegmentWithImage:segmentImage atIndex:i animated:NO];

This may help you
UIImage *segmentSelected = [[UIImage imageNamed:#"Off.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(6, 6, 6, 6)];
UIImage *segmentUnselected = [[UIImage imageNamed:#"On.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(6, 6, 6, 6)];
UIImage *segmentSelectedUnselected =
[UIImage imageNamed:#"dividerOn.png"];
UIImage *segUnselectedSelected =
[UIImage imageNamed:#"dividerOff.png"];
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segUnselectedSelected
forLeftSegmentState:UIControlStateNormal // | UIControlStateHighlighted
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segUnselectedSelected
forLeftSegmentState:UIControlStateHighlighted
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal //| UIControlStateHighlighted)
barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];
UIFont *font = [UIFont systemFontOfSize:16.0f];
UIColor *textColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
font, #"NSFontAttributeName",
textColor, #"NSForegroundColorAttributeName",
nil];
[[UISegmentedControl appearance] setTitleTextAttributes:attributes
forState:UIControlStateNormal];

simply change the segmentController tintcolor to UIColor clearColor, use style controlstyleborder
SegmentControl.backgroundColor= [UIColor clearColor];
[SegmentControl setTintColor:[UIColor clearColor]];

Related

UINavigationBar change title text color and font size

I want to change navigation title's font and color..so, for that i've done this below code..but its not working...
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7")) {
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
NSForegroundColorAttributeName,
[UIFont fontWithName:#"MyFavoriteFont" size:20.0],
NSFontAttributeName,
nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
NSLog(#"setTitleTextAttributes");
}
why this code is not working?
Apply Attributes to Navigationcontroller instance.
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7")) {
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
NSForegroundColorAttributeName,
[UIFont fontWithName:#"MyFavoriteFont" size:20.0],
NSFontAttributeName,
nil];
[self.transitionNavController.navigationBar setTitleTextAttributes:navbarTitleTextAttributes];
}
Hope this helps...
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:
#{
UITextAttributeTextColor: [UIColor whiteColor],UITextAttributeTextShadowColor: [UIColor clearColor],UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],UITextAttributeFont: [UIFont fontWithName:#"ArialMT" size:18.0f]
}];
CGFloat verticalOffset = -4;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
}
else
{
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
// Uncomment to change the color of back button
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// Uncomment to assign a custom backgroung image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];
// Uncomment to change the back indicator image
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#""]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#""]];
// Uncomment to change the font style of the title
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:#"ArialMT" size:18.0], NSFontAttributeName, nil]];
CGFloat verticalOffset = 0;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
}

change background image of navigation bar based on orientation

Is there a way to change the background image of navigation bar based on orientation? I tried using methods in the app delegate such as :
-(void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == (UIInterfaceOrientationMaskPortrait)||orientation==(UIInterfaceOrientationMaskPortraitUpsideDown)){
UIImage *myImage = [UIImage imageNamed:#"borderP.png"];
UIImage * imageBottom = [UIImage imageNamed:#"bottomP.png"];
[[UIToolbar appearance] setBackgroundImage:imageBottom forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:imageBottom forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
NSLog(#"******P******");
}
else if(orientation == (UIInterfaceOrientationMaskLandscapeLeft||orientation== UIInterfaceOrientationMaskLandscapeRight)){
UIImage *myImage = [UIImage imageNamed:#"borderL.png"];
UIImage * imageBottom = [UIImage imageNamed:#"borderL.png"];
[[UIToolbar appearance] setBackgroundImage:imageBottom forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:imageBottom forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
NSLog(#"******L********");
}
}
but the back ground image does not change.
Any advice?
The appearance proxy only applies only to new instances. To modify an instance that already exists, you you need to call setBackgroundImage:forToolbarPosition:barMetrics: on it directly.

Appearance proxy affected by upgrade to iOS 6

My app implements a custom segmented control background image using:
// customise the segmented controls
UIImage *segmentSelected = [[UIImage imageNamed:#"segcontrol_sel.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)];
UIImage *segmentUnselected = [[UIImage imageNamed:#"segcontrol_uns.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)];
UIImage *segmentSelectedUnselected = [UIImage imageNamed:#"segcontrol_sel-uns.png"];
UIImage *segUnselectedSelected = [UIImage imageNamed:#"segcontrol_uns-sel.png"];
UIImage *segmentUnselectedUnselected = [UIImage imageNamed:#"segcontrol_uns-uns.png"];
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected forState:UIControlStateDisabled barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentUnselectedUnselected forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segUnselectedSelected forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
It was perfectly working before I updated to XCode 4.5 and iOS6. Now it's like that:
as you can see the background images size were altered. Did any anybody have the same problem?
Also some of the background images for some screen look shorter the requested. In one of them for example I have a blank space.
Make sure you name your images with #2x if they're at that resolution.
I had a similar-looking problem which was fixed when I added that.

UISegmentedControl segment width of is not set when the background image is set

I have a segmented control and I want first and last items to be of specified width (say, wider than others). When I setWidth:forSegmentAtIndex: for standard-styled segmentedControl (i.e. [[UISegmantedControl appearence] setBackgroundImage:forState:barMetrics:] not set), things behave themselves as they should. But when I set background image, segments width doesn't change.
Here is my code:
[[UISegmentedControl appearance] setBackgroundImage:[[UIImage imageNamed:#"btn_normal.png"] stretchableImageWithLeftCapWidth:25 topCapHeight:0] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:[[UIImage imageNamed:#"btn_selected.png"] stretchableImageWithLeftCapWidth:25 topCapHeight:0] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:#"nn.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:#"sn.png"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:#"ns.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.segmentedControl setContentMode:UIViewContentModeScaleToFill];
[self.segmentedControl setWidth:100.0 forSegmentAtIndex:0];
[self.segmentedControl setWidth:100.0 forSegmentAtIndex:4];
With this, all my segments appear automatically sized to the equal width.
And when I comment out all above this
[self.segmentedControl setContentMode:UIViewContentModeScaleToFill];
[self.segmentedControl setWidth:100.0 forSegmentAtIndex:0];
[self.segmentedControl setWidth:100.0 forSegmentAtIndex:4];
the sizes are set correctly.
Here is my question: how can I set sizes for the segments with background images?
I'm new to cocoa-touch and objective-c, so I might be missing something...
Could you help please?
I think when you use [UISegmentedControl appearance] proxy it overrides "setWidth:forSegmentAtIndex:" method values. So possible solutions:
1)
[[UISegmentedControl appearance] setContentMode:UIViewContentModeScaleToFill];
[[UISegmentedControl appearance] setWidth:100.0 forSegmentAtIndex:0];
[[UISegmentedControl appearance] setWidth:100.0 forSegmentAtIndex:4];
I would not recommend to use this because it will set custom width for 0 and 4 segments for every segmented control in your app.
2)
[self.segmentedControl setBackgroundImage:[[UIImage imageNamed:#"btn_normal.png"] stretchableImageWithLeftCapWidth:25 topCapHeight:0] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:[[UIImage imageNamed:#"btn_selected.png"] stretchableImageWithLeftCapWidth:25 topCapHeight:0] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.segmentedControl setDividerImage:[UIImage imageNamed:#"nn.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setDividerImage:[UIImage imageNamed:#"sn.png"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setDividerImage:[UIImage imageNamed:#"ns.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
If you need that all segmented controls in your app should have custom background then I would recommend to write your own proxy method. Something like this:
- (UISegmentedControl *) customSegmentedControl {
UISegmentedControl *segmentedControl = [ [ [UISegmentedControl alloc] init] autorelease];
[segmentedControl setBackgroundImage: [ [UIImage imageNamed: #"btn_normal.png"] stretchableImageWithLeftCapWidth: 25 topCapHeight: 0] forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
[segmentedControl setBackgroundImage: [ [UIImage imageNamed: #"btn_selected.png"] stretchableImageWithLeftCapWidth: 25 topCapHeight: 0] forState: UIControlStateSelected barMetrics: UIBarMetricsDefault];
[segmentedControl setDividerImage: [UIImage imageNamed: #"nn.png"] forLeftSegmentState: UIControlStateNormal rightSegmentState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
[segmentedControl setDividerImage: [UIImage imageNamed: #"sn.png"] forLeftSegmentState: UIControlStateSelected rightSegmentState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
[segmentedControl setDividerImage: [UIImage imageNamed: #"ns.png"] forLeftSegmentState: UIControlStateNormal rightSegmentState: UIControlStateSelected barMetrics: UIBarMetricsDefault];
return segmentedControl;
}
I solved this by writing custom control, based on this http://idevrecipes.com/2010/12/11/custom-segmented-controls/

iOS5 TabBar Fonts and Color

I have customized the TabBar appearance such
UIImage *tabBackground = [[UIImage imageNamed:#"tab-bar-bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setBackgroundImage:tabBackground];
[[UITabBar appearance] setSelectionIndicatorImage: [UIImage imageNamed:#"activetab.png"]];
How do I define the custom fonts and the selected and unselected text colors?
Thanks,
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIFont fontWithName:#"font" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateHighlighted];
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIFont fontWithName:#"ProximaNova-Semibold" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateHighlighted];
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor grayColor], UITextAttributeTextColor,
[UIFont fontWithName:#"ProximaNova-Semibold" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
The above answer is working for me.
But I think most people should change the
forState:UIControlStateHighlighted to forstate:UIControlStateSelected