简体   繁体   中英

Programmatically add a UISegmentedControl to a UINavigationBar

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM