setting background image of UISegmentedControl - iphone

So I have the following code to set the segmented control background color:
UISegmentedControl * segmentedCtrl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 150, 35)];
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:#"btn-gradient-brown"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:#"btn-gradient-brown"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:#"icon-home.png"] atIndex:0 animated:NO];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:#"icon-star.png"] atIndex:1 animated:NO];
It all works fine except that when I click on the segment I can see the blue button highlighted. How do I disable this highlight? Here's a screenshot of my problem:

UISegmentControl has a Divider between two segments. By using the following code you can change the divider background.
[segmentedCtrl setDividerImage:[UIImage imageNamed:#"divider_selected.png"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedCtrl setDividerImage:[UIImage imageNamed:#"divider_normal.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Where divider_selected.png is a image which you used for selected state. Where as divider_normal.png is a image which you used for normal state.
Hope this will help....

Set your same background images for the highlighted state:
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:#"btn-gradient-brown"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

Related

customize UIBarButtonItem initWithBarButtonSystemItem to show up as grey not white in iOS 5

I am using a white background on my UINavigationBar with darkGrayColor text
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"uinavigationcontrollerbackground"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,UITextAttributeFont ,[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil]];
Which looks nice but I have buttons defined like this throughout my app:
self.searchButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(searchProducts)];
I killed the default blakc background look I had going like this:
[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:#"uibarbuttonbackground"] forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
Which is just a white square.
Now my icons are still white with a light gray shadow, anyway to change these default icons?
Once I tried to change background of UIBarButtonSystemItemCamera but nothing worked. I guess these default system button types (UIBarButtonSystemItem )come with a background image already, so changing it won't do.
I recommend you to create a custom view and init your bar button with it. Not a perfect code but I hope it helps.
UIImage *image = [UIImage imageNamed:#"icon.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:#selector(takePicture:) forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
[v addSubview:button];
UIBarButtonItem *barItem = [[[UIBarButtonItem alloc] initWithCustomView:v] autorelease];
I used the category solution found here, it is working great:
UIBarButtonItem with custom image and no border
I little more work than I was hoping but it is a nice clean solution.

Strange behavior of UIBarButtonItem when using UIAppearance

I'm customizing the elements like navigation bar, tab bar and uibarbuttonitem using UIAppearance. It works very well except for a very strange behavior of the UIBarButtonItem elements. On the top level of the navigation controller hierarchy everything looks good, but if I push the next view controller, the UIBarButtonItem elements move a little bit downwards, but at the same time, the back button stays at its correct position. I attached two images to illustrate my problem.
1) First view controller in the navigation controller hierarchy
2) Second view controller in the navigation controller hierarchy
EDIT: The code
//Change navigation bar appearance
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"GPNavigationBarBackground.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"GPNavigationBarShadow.png"]];
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont boldSystemFontOfSize:17], UITextAttributeFont,nil]];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:2.f forBarMetrics:UIBarMetricsDefault];
UIImage *buttonBackground = [[UIImage imageNamed:#"GPNavigationBarButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
UIImage *buttonPressedBackground = [[UIImage imageNamed:#"GPNavigationBarButtonPressed.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
UIImage *backButtonBackground = [[UIImage imageNamed:#"GPNavigationBarBackButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 10)];
UIImage *backButtonPressedBackground = [[UIImage imageNamed:#"GPNavigationBarBackButtonPressed.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 10)];
[[UIBarButtonItem appearance] setBackgroundImage:buttonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:buttonPressedBackground forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonPressedBackground forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundVerticalPositionAdjustment:1.f forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:1.f forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, 1.f) forBarMetrics:UIBarMetricsDefault];
Well, after brooding on that problem, I finally solved it. The code above is absolutely correct. The only problem was the height of the background images for the UIBarButtonItem. The UIAppearance proxy lets you set all the graphics, but it doesn't allow you to alter the height of the UIBarButtonItem.
So, when customizing UIBarButtonItem, always remember that a
UIBarButtonItem can't be higher than 30pt. That means that custom artwork shouldn't exceed this size.
You can set background images that are higher than 30pt, but you will run into the same problem as stated above.

UISegmentedControl with buttons

So I have a UISegmentedControl with custom backgroundImage.. Here's how I set it up:
UISegmentedControl * segmentedCtrl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 178, 30)];
[segmentedCtrl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segmentedCtrl setTintColor:[UIColor clearColor]];
[segmentedCtrl setSegmentedControlStyle:UISegmentedControlStyleBezeled];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:#"btn-home-down.png"] atIndex:0 animated:NO];
.....
[segmentedControl setImage:[UIImage imageNamed:#"btn-home.png"] forSegmentAtIndex:0];
[segmentedControl setImage:[UIImage imageNamed:#"btn-popular.png"] forSegmentAtIndex:1];
[segmentedControl setImage:[UIImage imageNamed:#"btn-my-photos.png"] forSegmentAtIndex:2];
[segmentedControl setImage:[UIImage imageNamed:#"btn-places.png"] forSegmentAtIndex:3];
[segmentedControl setImage:[UIImage imageNamed:#"btn-my-likes"] forSegmentAtIndex:4];
Now the issue is this:
If you see closely between the segment image, on the divider line at the bottom I sort of have a white dot. All of my images are set to square.. why is this and how can I eliminate this?
Maybe you could get a UIImageView with only one pixel colored in with the color that the white pixel should be. Then you could placed it on top of the white pixel. This probably isn't the best way of doing this, but it works.
Hope this helps.

How can I make setEnabled work on a customised UISegmentedControl in iOS5?

I have a UISegmentedControl in my iPad app which I have customised with the new methods available in iOS5, as below:
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected forState:UIControlStateNormal 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];
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], UITextAttributeFont,
[UIColor colorWithRed:0.3 green:0.34 blue:0.42 alpha:1], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
CGSizeMake(0, 1), UITextAttributeTextShadowOffset, nil];
[[UISegmentedControl appearance] setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
It looks fine and works ok, but there's a problem in that calling setEnabled:NO on any of the segments has no effect - the segment will still respond to touch events. Does anybody know what I need to do to disable some of the segments?
Use isEnabledForSegmentAtIndex: and setEnabled:forSegmentAtIndex: found in the documentation for UISegmentedControl. Let me know if that gets it working.
Not sure if this is still an issue for anyone (since it's fixed in iOS 6, but here's the workaround that worked from me (borrowed from another question):
In your viewDidLoad, try:
dispatch_async(dispatch_get_main_queue(),^{
[self.segmentedControl setEnabled:NO forSegmentAtIndex:2];
});
It seems that when the views load, the presence of an appearance proxy resets the other properties of the UISegmentedControl. Scheduling this on the main thread will re-enable/re-disable. This also BTW works for selecting the default segment.

How can I adjust the highlight color of my UIBarButtonItem?

I am using the appearance proxy to adjust my color settings globally:
//Setup custom appearances
if ([UINavigationBar respondsToSelector:#selector(appearance)]) {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"header"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:96.0/255.0 green:13.0/255.0 blue:11.0/255.0 alpha:1.0]];
[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:96.0/255.0 green:13.0/255.0 blue:11.0/255.0 alpha:1.0]];
//rgb: 96, 13, 11
//[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"header"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setTintColor:[UIColor blackColor]];
}
This works fine, but when I tap on a UIBarButtonItem that is in my UINavigationbar, it changes color to black. How can I make sure the highlighted state is not black?
last line is:
[[UIToolbar appearance] setTintColor:[UIColor blackColor]];
maybe rem it?
Check this out: UIBarButtonItem with color?
Make sure that you import "QuartzCore/QuartzCore.h" in order to access the layer properties of UIView. To import that, you need to have added the CoreGraphics framework to your project.