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.
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.
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.
“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.”
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.
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
You have to set the content size of the UIScrollView
, otherwise it won't scroll.
myScrollView.contentSize = mySegmentedControl.frame.size;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With