NSArray *itemArray = [NSArray arrayWithObjects:#"one", #"Two", #"Three", nil];
segmentControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentControl.frame = CGRectMake(5, 5, 325, 35);
segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentControl.tintColor = [UIColor blackColor];
[self changeUISegmentFont:segmentControl];
//[self.view addSubview:segmentControl];
self.navigationItem.titleView = segmentControl;
[segmentControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
This is my code of segment control and my problem is ..
When I select any segment it does not show me that that segment is selected..
I mean it is not shown highlighted..
can Anyone tell me what the problem is??
Change the following line in your code.
segmentControl.tintColor = [UIColor blackColor];
to
segmentControl.tintColor = [UIColor grayColor];
Actually the segment is getting selected but It is not visible to you, because black color is the default color in selected mode.
Related
Ive added a segmented control to the header of a uitableview. this works fine. but for some reason i cannot make the segmented buttons (or at least just the first button) have a red background color. it just loads with the default silver.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* NEWview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
NEWview.backgroundColor = [UIColor colorWithRed:78.0/255.0f green:88.0/255.0f blue:74.0/255.0f alpha:1.0];
NSArray *itemArray = [NSArray arrayWithObjects: #"Organisations", #"Events", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(15, 5, 290, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;
UIColor *newSelectedTintColor = [UIColor redColor];
[[[segmentedControl subviews] objectAtIndex:0] setTintColor:newSelectedTintColor];
[NEWview addSubview:segmentedControl];
return NEWview;
}
Any Ideas? Thanks in advance for any help..
Tint color for segmentedcontrol works only if segmented control is of bar style.
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor=[UIColor redColor];
Refer this link to changed background color of selected segment
U can also do is set title color slected and not selected like this:
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Arial" size:20.0],UITextAttributeFont,
[UIColor colorWithRed:75.0/255.0 green:75.0/255.0 blue:75.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
nil];//[NSDictionary dictionaryWithObject: [UIColor redColor]forKey:UITextAttributeTextColor];
[segmentCtrl setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Arial" size:20.0],UITextAttributeFont,
[UIColor redColor], UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
nil] ;//[NSDictionary dictionaryWithObject: [UIColor redColor]forKey:UITextAttributeTextColor];
[segmentCtrl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
I created a UISegmentedControl through the following snippet.
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(105, 270, 140, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(selectWeek:) forControlEvents:UIControlEventValueChanged];
[self.navigationController.view addSubview:segmentedControl];
When I press the button to turn UISegmentedCotrol, it doesn't work. What could be wrong?
I'm doing segmentedControl.hidden = YES;.
[self.navigationController.view addSubview:segmentedControl];
change it to
[self.view addSubview:segmentedControl];
and make sure segmentedControl.hidden=NO; when you add it to subview;
I have an UISegmentedControl as the navigation bar's right bar button item. This is achieved by the following code...
UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Segment1",#"Segment2",nil]];
[segmentedControl addTarget:self action:#selector(segmentClicked) forControlEvents:UIControlEventValueChanged]; //Where segmentClicked is the method for segment click action
segmentedControl.frame = CGRectMake(0, 0, 90, 35);
UIBarButtonItem *rightBaritem = [ [UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = rightBaritem;
[rightBaritem release];
The above code fine and it will show a segmented Control with two segments "Segment1" & "Segment2".
But I want to show an image instead of Segment1 & a system bar button (Say UIBarButtonSystemItemAdd) instead of Segment2.
Image can be inserted in Segmented control by the code,
UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:[UIImage imageNamed:#"<image_name.image_type>"],???????,nil]];
But i dont know how to include UIBarButtonSystemItemAdd in the place of ???????.
Thanks in Advance..
This code may solve your problem.This solved my problem that I was facing at the same time.But don't think this is the accurate one.I got the desired which I need to display.This works fine with two buttons.
UISegmentedControl *doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
doneButton.momentary = YES;
doneButton.frame = CGRectMake(240, 7.0f, 70.0f, 30.0f);
doneButton.tintColor = [UIColor blackColor];
[doneButton addTarget:self action:#selector(Done:) forControlEvents:UIControlEventValueChanged];
[menustyle addSubview:doneButton];
[doneButton release];
doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
doneButton.momentary = YES;
doneButton.frame = CGRectMake(10, 7.0f, 70.0f, 30.0f);
doneButton.tintColor = [UIColor blackColor];
[doneButton addTarget:self action:#selector(Krishna:) forControlEvents:UIControlEventValueChanged];
[menustyle addSubview:doneButton];
[doneButton release];
Is this an heresy to add a UISegmentedControl to a navigation Controller Toolbar?
I am considering this code:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[NSString stringWithString:NSLocalizedString(#"One", #"")],
[NSString stringWithString:NSLocalizedString(#"Two", #"")],
[NSString stringWithString:NSLocalizedString(#"Three", #"")],
[NSString stringWithString:NSLocalizedString(#"Four", #"")],
nil]];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor blackColor];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl addTarget:self action:#selector(changeSegment:)
forControlEvents:UIControlEventValueChanged];
[segmentedControl setFrame:[self.navigationController.toolbar bounds]];
[self.navigationController.toolbar addSubview:segmentedControl];
[segmentedControl release];
I have tried this code and the segments don't appear to be selected with I touch them.
Is this the correct way of doing that?
thanks.
This is most likely due to your tintColor being black. Change the color and you should see the selected segment reflected.
I have one problem :I have UISegmentedControl with three indexes 0,1,2. when i select the index of UISegmentedControl it is working fine but I need to have multiple touches and it should be focued How it can be achieved. how can be programed for Tapping the selected index again and it should work...
NSArray *segmentTextContent = [NSArray arrayWithObjects: allString, favoritesString,filterString, nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
[segmentedControl setFrame:CGRectMake(60,10,200,30)];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//segmentedControl.momentary = YES;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor=[UIColor colorWithRed:36/255.0 green:61/255.0 blue:103/255.0 alpha:1];
segmentedControl.selectedSegmentIndex =0;
[toolBar1 addSubview:segmentedControl];
mySegmentedControl.momentary = YES;
I believe this is want you want.