customize UISegmentedControl with resizableImageWithCapInsets - ios

I am customize UISegmentedControl with custom background images.
Code is like this:
UIImage *segmentSelected = [[UIImage imageNamed:#"segcontrol_sel.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
UIImage *segmentUnselected = [[UIImage imageNamed:#"segcontrol_uns.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
UIImage *segmentSelectedUnselected = [[UIImage imageNamed:#"segcontrol_sel-uns.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
UIImage *segUnselectedSelected = [[UIImage imageNamed:#"segcontrol_uns-sel.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
UIImage *segmentUnselectedUnselected = [[UIImage imageNamed:#"segcontrol_uns-uns.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[[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];
But the result is not what it is supposed to be. I am not very sure about the usage of the method: resizableImageWithCapInsets.
image is attached.
it should be like this:
but actually it is like this:
I guess I used the wrong UIEdgeInset value.
The background image is of width: 22; height: 30
and the divider image is of width: 11; height: 30
Any suggestion?

Try this
HMSSegmentControl
only use these two files and its selected and unselect images
HMSegmentedControl.h
HMSegmentedControl.m
.h file
#import "HMSegmentedControl.h"
HMSegmentedControl *segmentedControl;
.m file
code for viewDidLoad
segmentedControl = [[HMSegmentedControl alloc] initWithSectionImages:#[[UIImage imageNamed:#"1"], [UIImage imageNamed:#"2"], [UIImage imageNamed:#"3"]] sectionSelectedImages:#[[UIImage imageNamed:#"1-selected"], [UIImage imageNamed:#"2-selected"], [UIImage imageNamed:#"3-selected"]]]; // it create size as per your segment total image
[segmentedControl setSelectionIndicatorHeight:4.0f];
[segmentedControl setFrame:CGRectMake(0, 45, 320, 45)];
[segmentedControl setSegmentEdgeInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[segmentedControl addTarget:self action:#selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
[segmentedControl setBackgroundColor:[UIColor blueColor]];
[segmentedControl setSelectionLocation:HMSegmentedControlSelectionLocationDown];
[segmentedControl setSelectionStyle:HMSegmentedControlSelectionStyleTextWidthStrip];
[self.view addSubview:segmentedControl];
- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl1 {
if (segmentedControl1.selectedSegmentIndex==0) {
}
else if (segmentedControl1.selectedSegmentIndex==1) {
}
else if(segmentedControl1.selectedSegmentIndex==2) {
}
}

I think that you should to use higher quality images.
I guest i used the wrong UIEdgeInset value. The background image is of width: 22; height: 30 and the divider image is of width: 11; height: 30 Any suggestion?
You should try 60 * 60 px for your images.

Related

Customizing UIBarButton makes it too big

So when I try to set a custom color to a UIBarButton, it causes the displayed button to be too big and it extends down past the NavigationBar and into the main view of the app. Is there an easy way to fix this? Here is my code:
UIImage *buttonColor = [[UIImage imageNamed:#"blue-background.jpg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[[UIBarButtonItem appearance] setBackgroundImage:buttonColor forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
try playing with the values of the UIEdgeInsetsMake function.
in one of my apps it looks like this;
UIImage *backbuttonimage = [[UIImage imageNamed:#"backbutton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 10)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backbuttonimage
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
it made it as i wanted... if you have any problems i would like you to tell me and if you can add a photo of how it looks for me to understand better the problem.
these are my values for my app:
UIEdgeInsetsMake(0, 15, 0, 10)
have a nice day :)
This is how i make custom UIBarButtonItem and it works perfect for me. Try this:
UIButton *postView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 61, 30)];
[postView setTitle:#"Post" forState:UIControlStateNormal];
[postView addTarget:self action:#selector(showAddPost) forControlEvents:UIControlEventTouchUpInside];
[postView setBackgroundImage:[UIImage imageNamed:#"bbi_texture.png"] forState:UIControlStateNormal];
[postView setBackgroundImage:[UIImage imageNamed:#"bbi_texture.png"] forState:UIControlStateHighlighted];
UIBarButtonItem *postButton = [[UIBarButtonItem alloc] initWithCustomView:postView];
[self.navigationItem setRightBarButtonItem:postButton];

UISegmentedControl wrong dividerImage

I am custumizing all segmented controls in my app with the following code.Initially I set the selected segment to index 2.
Everything works perfect in IOS 6.While I was testing the app on IOS5, I realised that the initial setting of segmented control had a bug.The separation image between selected and unselected state is not set right.Due to that it looks like this.
If i change the selected segments by tapping the segmentedcontrol behaves normal.It is very weird.What elsse shall i do to prevent this strange behaviour?
UISegmentedControl *localSegmentedControl = [[UISegmentedControl alloc] init];
if ([localSegmentedControl respondsToSelector:#selector(setBackgroundImage:forState:barMetrics:)]) {
UIImage *segmentUnselectedSelectedDivider = [UIImage imageNamed:#"segmentedControlSeperatorNS.png"];
UIImage *segmentSelectedUnselectedDivider = [UIImage imageNamed:#"segmentedControlSeperatorSN.png"];
UIImage *segmentUnselectedUnselectedDivider = [UIImage imageNamed:#"segmentedControlSeperatorNN.png"];
UIImage *segmentUnselected = [[UIImage imageNamed:#"barButtonPlain.png"] stretchableImageWithLeftCapWidth:7 topCapHeight:0];
UIImage *segmentSelected = [[UIImage imageNamed:#"doneButton.png"] stretchableImageWithLeftCapWidth:7 topCapHeight:0];
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentUnselectedUnselectedDivider
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselectedDivider
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentUnselectedSelectedDivider
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
}
After trying all relevant approached explained in Customizing UISegmentedControl in iOS 5
i figured out that the problem is related with the width of the separation line.The tutorial about segmented control customisation in http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5 assumes that the separation images are wider than 2px (in Retina).
I made them exactly 2px wide and the problem is resolved.
I think it is a known bug.
It is the same here with a workaround, may work for you.
Customizing UISegmentedControl in iOS 5
When i use segment control I was very tired the same problem. I solved that code
UIImage *segmentSelected =
[[UIImage imageNamed:#"ikisiSecildiKirmizi.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 5, 15)];
UIImage *segmentUnselected =
[[UIImage imageNamed:#"ikisiSecilmediGri.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 5, 15)];
UIImage *segmentSelectedUnselected =
[[UIImage imageNamed:#"solSecili.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 5, 15)];
UIImage *segUnselectedSelected =
[[UIImage imageNamed:#"sagSecili.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 5, 15)];
UIImage *segmentUnselectedUnselected =
[[UIImage imageNamed:#"ikisideSecilmemis.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 5, 15)];
[fiyatSaat setBackgroundImage:segmentUnselected
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[fiyatSaat setBackgroundImage:segmentSelected
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[fiyatSaat setDividerImage:segmentUnselectedUnselected
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[fiyatSaat setDividerImage:segmentSelectedUnselected
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[fiyatSaat setDividerImage:segUnselectedSelected
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIColor blackColor]
forKey:UITextAttributeTextColor];
[fiyatSaat setTitleTextAttributes:attributes
forState:UIControlStateNormal];
NSDictionary *attributes2 = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
forKey:UITextAttributeTextColor];
[fiyatSaat setTitleTextAttributes:attributes2
forState:UIControlStateHighlighted];

I want to change UISegmentedControl images after selection

I have UISegmentedControl object in which I took images array. Now I want to change this images after selection of each object like UIButton control in which we can set image for selection and also for non selection.
Here is my code
NSArray *itemArray;
itemArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"grey.png"],
[UIImage imageNamed:#"overlay.png"],
[UIImage imageNamed:#"marker.png"],
nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:#selector(segmentedControlIndexChanged) forControlEvents:UIControlEventValueChanged];
You can customise segment control with your own images for selected state and normal state in iOS 5
Try this:
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: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];
You can also Check Complete UI customisation at http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

Multiple uisegmentedcontrol in 1 view not working

I have a view with 2 uisegmentedcontrols, one along the bottom and one in the nav bar. The bottom one is working fine, the one in the nav bar appears, but does not pick up any touch events. Code for the bottom segment (working) is:
footerSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"1", #"2", nil]];
[footerSegment setFrame:CGRectMake(5, 6, 310, 30)];
[footerSegment setSegmentedControlStyle:UISegmentedControlStyleBar];
footerSegment.selectedSegmentIndex = 0;
//segmentFiltering.tintColor = [UIColor colorWithWhite:80.0/255.0 alpha:1.0];
[footerSegment addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[footerSegment setTag:0];
[footerSegment setBackgroundColor:[UIColor clearColor]];
UIImage *segSelected = [[UIImage imageNamed:#"segment_sel.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0];
UIImage* menuRightImage = [[UIImage imageNamed:#"button.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
[footerSegment setBackgroundImage:menuRightImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[footerSegment setBackgroundImage:segSelected
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
UIImage *segmentDividerImg = [[UIImage imageNamed:#"div.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
// Image between two unselected segments.
[footerSegment setDividerImage:segmentDividerImg forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
// Image between segment selected on the left and unselected on the right.
[footerSegment setDividerImage:segmentDividerImg forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
// Image between segment selected on the right and unselected on the right.
[footerSegment setDividerImage:segmentDividerImg forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[footerToolBar addSubview:footerSegment];
Code for the navbar one is below (visable but doesn't work):
headSegment = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:#"A", #"B", nil]];
[headSegment setFrame:CGRectMake(50, 7, 200, 29)];
[headSegment setSegmentedControlStyle:UISegmentedControlStyleBar];
headSegment.selectedSegmentIndex = 0;
[headSegment addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[headSegment setBackgroundColor:[UIColor clearColor]];
[headSegment setBackgroundImage:menuRightImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[headSegment setBackgroundImage:segSelected forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[headSegment setDividerImage:segmentDividerImg forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[headSegment setDividerImage:segmentDividerImg forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[headSegment setDividerImage:segmentDividerImg forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[headSegment setTag:1];
[self.navigationController.navigationBar addSubview:headSegment];
Code for the action:
-(void)segmentAction:(UISegmentedControl*)sender
{
if(sender.tag == 0){
NSLog(#"Selected index: %d", sender.selectedSegmentIndex);
}
else{
if (sender.selectedSegmentIndex = 1) {
NSLog(#"Selected HEAD index: %d", sender.selectedSegmentIndex);
}
}
[self.navigationController.navigationBar addSubview:headSegment];
That is not how you add things to a navigation bar. You must work by way of your navigation controller's navigationItem. You can make the segmented control its titleView or you can wrap it in a bar button item and make that one of the right or left buttons.

iPhone UINavigationBar - Set width and margin of rightBarButtonItem

I have a background image for a button, with the dimensions of 80x30 pixels.
I use the code below to set the background in my view-controller, and the result is this:
As you can see, the corners and the top of the right button is messed up.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIImage *favoriteBtnImgNormal = [[UIImage imageNamed:#"favorite-btn-normal"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 30, 0)];
UIImage *favoriteBtnImgTouch = [[UIImage imageNamed:#"favorite-btn-touch"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 30, 0)];
[self.navigationItem.rightBarButtonItem setBackgroundImage:favoriteBtnImgNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setBackgroundImage:favoriteBtnImgTouch forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setBackgroundImage:favoriteBtnImgTouch forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setTitlePositionAdjustment:UIOffsetMake(-10.0, 0.0) forBarMetrics:UIBarMetricsDefault];
}
Note that I do this in a single view-controller, I use the appearance API to set general styles. But here I want to override the general appearance.
If I just get the images without the resizable-stuff, it looks like this:
Now the edges and corners are fine, but the button is too large.
I'm sure I'm doing this all wrong, so I need someone to point out what I could do to scale buttons correctly?
UIImage *image = [UIImage imageNamed:#"YourImage"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(YourAction) forControlEvents:UIControlEventTouchUpInside];
button.contentEdgeInsets = (UIEdgeInsets){.right=-10};
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

Resources