Segmented control inside scroll

I am trying to create a very long segmented controller so that users can choose between many options. How can I get it inside the scroll?

I tried to drag it, but that does not allow me to scroll it.

+6
source share
6 answers

Try adding a segmented control as a scroll 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 is a post on how to create a segmented control in a scroll view

http://penningthoughtsandmemoirs.com/2013/12/16/sliding-segmented-control/

Download the source code:

https://github.com/sahilriaz1110/SlidingSegmentedControl

+5
source

You must set the size of the contents of the UIScrollView , otherwise it will not scroll.

 myScrollView.contentSize = mySegmentedControl.frame.size; 
+1
source

Instead of suggesting how to put a segmented control in a scrollview (see other answers), I'm going to suggest a different approach: change the user interface element.

If you have a couple (from two to three) mutually exclusive options on the desktop application, you can use the radio buttons, and this will make sense; but if you had ten or more options, throwing away these switches in a scrollview would not be the best choice. The better / cleaner user interface will use the drop-down menu.

This was for a desktop application. But the principles of the user interface for mobile OS are the same. A segment control should have several options (maybe five vertices). Moreover, you should use a different user interface element.

Imagine that iPhone, when choosing a language, offered each language in a segmented control. No! Instead, when you select a language, you will be shown a list of choices.

+1
source

Do you use autorun? If so, you will need to set the scrollView content size from viewDidLayoutSubviews. How:

 - (void) viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [scrollView setContentSize:[segControl frame].size]; } 

If you do not use autostart, you can simply do this in -(void)viewDidLoad , for example:

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [scrollView setContentSize:[segControl frame].size]; } 

EDIT . You can also make scrollview contentSize a little smaller than your segmented control, so there is no value in the vertical direction:

 [scrollView setContentSize:CGSizeMake([segControl frame].size.width, [segControl frame].size.height-1)]; 
0
source

If UISegmentedControl width 640 , use:

 [yourScrollView setContentSize:CGSizeMake([segControl frame].size.width, 0)]; 

Using this line will scroll it horizontally to select another option on the UISegmentedControl .

0
source

In UIBuilder, you can add a ScrollView, which is part of the ViewController, and populate it with a segmented control that is larger than the screen.

Segmented Control StoryboardSegmented Control UI Properties

-1
source

Source: https://habr.com/ru/post/949146/


All Articles