I'm trying to create a really long segmented controller to let users choose between many options. How can I go about getting it inside of a Scroll View?
I've tried dragging and dropping it but it doesn't let me scroll it.
I'm trying to create a really long segmented controller to let users choose between many options. How can I go about getting it inside of a Scroll View?
I've tried dragging and dropping it but it doesn't let me scroll it.
Try adding a segmented control in a scroll view by this code :
- (void)viewDidLoad { [super viewDidLoad]; journals = [[NSMutableArray alloc]init]; self.tableView.dataSource = self; self.tableView.delegate = self; UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 49, 320, 29)]; self.segmentedControl.frame = CGRectMake(0, 0, 640, 29); scrollView.contentSize = CGSizeMake(self.segmentedControl.frame.size.width, self.segmentedControl.frame.size.height -1); scrollView.showsHorizontalScrollIndicator = NO; self.segmentedControl.selectedSegmentIndex = 0; [scrollView addSubview:self.segmentedControl]; [self.view addSubview:scrollView]; [self fillJournals]; // Do any additional setup after loading the view, typically from a nib. } Here's a post on how to create a segmented control inside a scroll view
http://penningthoughtsandmemoirs.com/2013/12/16/sliding-segmented-control/
Download the source code from :
Instead of suggesting how to put the segmented control in a scrollview (see the other answers listed) I'm going to suggest a different approach altogether: change UI element.
If you had a couple (two to three) mutually exclusive options on a desktop application, you could use radio buttons and it would make sense; but if you had ten and above options, throwing these radio button in a scrollview would not be the best choice. A better/cleaner user interface would make use of a drop-down menu.
That was for a desktop application.. but the UI principles for mobile OSes are the same. A segment control should have few options (maybe five tops). Any more than that and you should use a different UI element.
Imagine if the iPhone, when selecting a language, offered every language in a segmented control. No! Instead, when choosing a language, you're shown a list to choose from.
Are you using autolayout? If so then you will need to set the scrollView's content size from within viewDidLayoutSubviews. Like:
- (void) viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [scrollView setContentSize:[segControl frame].size]; } If you aren't using autolayout then you can just do it in -(void)viewDidLoad like:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [scrollView setContentSize:[segControl frame].size]; } EDIT You may also want to make the scrollview contentSize slightly smaller than your segmented control so there is no give in the vertical direction:
[scrollView setContentSize:CGSizeMake([segControl frame].size.width, [segControl frame].size.height-1)];