Is it possible to have a UISegmentedControl stick when it is selected, rather than just act as a normal button? In this sense they would be more like a UISwitch.
Here is my code:
segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:#"1",#"2",#"3",
nil]];
segmentedControl.frame = CGRectMake(0, 0, 120, 30);
[segmentedControl setWidth:40.0 forSegmentAtIndex:0];
[segmentedControl setWidth:40.0 forSegmentAtIndex:1];
[segmentedControl setWidth:40.0 forSegmentAtIndex:2];
[segmentedControl addTarget:self action:#selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.momentary = YES;
self.navigationItem.titleView = segmentedControl;
Here is a video of what is happening: https://www.dropbox.com/s/laijgt3zjdaya1z/Segmented.mov?dl=0
A UISegmentedControl "sticks" when it is selected by default. You're getting the behavior you describe because you set the momentary property to YES. The default is NO, so just delete that line.
Related
I am trying to add segmented control to tableviewheader. I am using the following code to do that
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"ALL", #"HOUSE", #"SENATE", nil]];
segmentedControl.frame = CGRectMake(24, 0, 272, 30);
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor grayColor];
[self.view addSubview:segmentedControl];
segmentedControl.selectedSegmentIndex = 0;
self.tableView.tableHeaderView = segmentedControl;
I can't set the frame, no matter what value is put its always 100% wide. How can i set the add 24px margin on left and right ?
That is because you are using the segment control as a tableHeaderView. a UITableViewHeaderView will be always be as wide as your tableView. You can only change the height.
Adding to akshaynhegde answer, you can easily achieve this by adding an extra UIView as parent and add the UISegmentControl to that UIView. In this case the parent UIView will take the whole width of UITableView but not the UISegmentControl.
So following should be the change in your code
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0,0,320.0,30.0)];
[view addSubview:segmentedControl];
self.tableView.tableHeaderView = view;
It should solve your problem.
Cheers.
First, you are adding segmentedControl on controller view [self.view addSubview:segmentedControl] and then assigning it to table header view
self.tableView.tableHeaderView = segmentedControl;
Which is wrong, you need to create a view with size table header size and then add segmentedControl add on view and assign this view to table header view.
Example
**UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"ALL", #"HOUSE", #"SENATE", nil]];
segmentedControl.frame = CGRectMake(24, 0, 272, 30);
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor grayColor];
[view addSubview:segmentedControl];
self.tableView.tableHeaderView = view;**
How do I PROGRAMMATICALLY add a UISegmentedControl to a UINavigationBar?
I do not want to use a XIB file for this.
I have a UIView with a UITableView that is added as a subview.
I have tried two methods but both are not satisfactory:
1)
self.segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"All",#"Subject",#"Category",#"Finished",nil]];
self.segmentedControl.backgroundColor = [UIColor cloudsColor];
[self.segmentedControl setSelectedSegmentIndex:0];
[self.segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue) forControlEvents:UIControlEventValueChanged];
self.mainView.tableHeaderView = self.segmentedControl;
The reason this first one fails is that in the UITableView, when the user scrolls, the segmented control is scrolled as well! I don't want that to happen. It must stay pinned to the top.
2) Second attempt
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Filter_Personnal", #"Filter_Department", #"Filter_Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
[statFilter sizeToFit];
self.navigationItem.titleView = statFilter;
This removes my title!!! I have a title at the top in the UINavigationBar and this method removes it!!
Here's an example of what I want to accomplish: UISegmentedControl below UINavigationbar in iOS 7
The UISegmentedControl must stay pinned below as part of the UINavigationBar and it must be below the title!
Thanks!
Use tableView:viewForHeaderInSection: (and possibly tableView:heightForHeaderInSection:) instead of tableHeaderView. Set tableStyle to UITableViewStylePlain (this should be the default).
You can use the following code :
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section == 0) {
UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"All",#"Subject",#"Category",#"Finished",nil]];
segmentedControl.backgroundColor = [UIColor cloudsColor];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 20, viewHeader.frame.size.width, 50);
[viewHeader addSubview:segmentedControl];
return viewHeader;
}
return nil;
}
Hope this helps.
Thanks.
As the other posters have suggested, you could put the segmented control above the table view and under the nav bar, but you'd need to shift down the table view.
...Or, you could add the segmented control as a tableHeaderView.
A third option is to actually add it to the navigation bar. To do that you have to turn it into a navBarItem. Something like this:
UISegmentedControl *statFilter = [[UISegmentedControl alloc]
initWithItems:
[NSArray arrayWithObjects:
#"Filter_Personnal",
#"Filter_Department",
#"Filter_Company",
nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
[statFilter sizeToFit];
UIBarItem *theBarItem = [[UIBarItem alloc] initWithCustomView: statFilter];
self.navigationItem.rightBarButtonItem = theBarItem;
Trying to set the selected side of a UISegmentControl, but nothing happens.
In my viewDidLoad or viewDidAppear this code will not select a segment?
[self.segmentedControl setSelectedSegmentIndex:[#1 integerValue]];
Nothing happens?
I deleted the UISegmentControl from the Storyboard and added it programmatically and it works now. Very weird.
self.segControl.frame = CGRectMake(35, 100, 200, 50);
self.segControl.segmentedControlStyle = UISegmentedControlStylePlain;
self.segControl.selectedSegmentIndex = 1;
[self.view addSubview:self.segControl];
You can use code for adding and setting selected tab in UISegmentControl
NSArray *itemArray = [NSArray arrayWithObjects: #"Top Twenty", #"Dow thirty", #"Favorite", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(15, 80, 300, 30);
segmentedControl.backgroundColor = [UIColor groupTableViewBackgroundColor];
[segmentedControl addTarget:self action:#selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0; //**this line represent selected tab**
[self.view addSubview:segmentedControl];
How do I pin a UISegmentedControl underneath a UINavigationBar with a UITableView?
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"One", #"Two", nil]];
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
self.tableView.tableHeaderView = segmentedControl;
I've set up this UISegmentedControl like this so far, however, it scrolls with the rest of the UITableView content.
Try to use segmentedControl as your section header view.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"One", #"Two", nil]];
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
return segmentedControl ;
}
Try the below code
NSArray *keyWordsList;
segmentControl = [[[UISegmentedControl alloc] initWithItems:keyWordsList] autorelease];
[segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segmentControl setFrame:CGRectMake(10, 47, 300, 30)];
[segmentControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[segmentControl addTarget:self action:#selector(segmentColorAction:) forControlEvents:UIControlEventValueChanged];
[segmentControl setSelectedSegmentIndex:0];
segmentControl.tag = 2;
//[self.navigationController.navigationBar addSubview:segmentControl];
[self.view addSubview:segmentControl];
segmentControl.hidden =YES;
[segmentControl setTag:kTagFirst];
kTagFirst provide value
Here's what I see on the simulator:
Here's what I see on the device:
This is the code:
- (void)buildNavBarTitle
{
self.navigationItem.title = nil;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, self.navigationController.navigationBar.frame.size.height)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 160, view.frame.size.height - 10)];
segmentedControl.tintColor = [UIColor blackColor];
[segmentedControl insertSegmentWithTitle:#"New" atIndex:0 animated:NO];
[segmentedControl insertSegmentWithTitle:#"Today" atIndex:1 animated:NO];
[view addSubview:segmentedControl];
self.navigationItem.titleView = view;
}
I have
deleted app on simulator & device
restarted xCode
cmd+shift+k to clean
cmd+shift+alt+k to wipe build folder
I ran into a similar issue when I used UIAppearance to style all controls of that type. It had to do with when I was trying to remove the shadow from the text, it cause the text not to appear. After allowing the shadow, you couldn't notice that it was really there, and the text reappeared.
I have submitted a bug to apple regarding this.
Try this:
NSArray *itemArray = [NSArray arrayWithObjects: #"New", #"Today", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray]
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 0;
[view addSubview:segmentedControl];