Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segmented Control inside of Scroll View

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.

like image 369
centree Avatar asked Jul 10 '13 17:07

centree


People also ask

How do you change view controllers using segmented control?

To switch between the child view controllers, we use a segmented control. Click the + button in the top right to bring up the Library and add a segmented control to the navigation bar of the master view controller. Open MasterViewController. swift and create an outlet for the segmented control.

What is paging in scroll view?

Scroll Views in combination with paging enabled allows the user to scroll page by page. In this tutorial we will create some views, which can be scrolled using paging. This tutorial is made with Xcode 10 and built for iOS 12. Open Xcode and create a new Single View App.

What is segment control in Swift?

“Segment control is a set of two or more segments, which is used to perform multiple tasks on the same screen. Segment working same as a button.”

What is UI scroll view?

UIScrollView is the superclass of several UIKit classes, including UITableView and UITextView . A scroll view is a view with an origin that's adjustable over the content view. It clips the content to its frame, which generally (but not necessarily) coincides with that of the app's main window.


2 Answers

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 :

https://github.com/sahilriaz1110/SlidingSegmentedControl

like image 80
penningthoughts Avatar answered Oct 01 '22 04:10

penningthoughts


You have to set the content size of the UIScrollView, otherwise it won't scroll.

myScrollView.contentSize = mySegmentedControl.frame.size;
like image 45
Levi Avatar answered Oct 01 '22 05:10

Levi