Use segment controller and UIScrollView in iOS
Specify the Root View:
// Set the window's Root View Controller self. window. rootViewController = [[UINavigationController alloc] initWithRootViewController: [RootViewController new];
Define attributes
#import RootViewController.h#import FirstViewController.h#import SecondTableViewController.h@interface RootViewController ()
@property (nonatomic, strong) UISegmentedControl *segmentedControl;@property (nonatomic, strong) UIScrollView *scrollView;@property (nonatomic, strong) FirstViewController *firstVC;@property (nonatomic, strong) SecondTableViewController *secondTVC;@end@implementation RootViewController
Creation implementation:
-(Void) viewDidLoad {[super viewDidLoad]; // applicable to scrollView self. automaticallyAdjustsScrollViewInsets = NO; self. segmentedControl = [[UISegmentedControl alloc] initWithItems: @ [@ first, @ second]; self. navigationItem. titleView = self. segmentedControl; [self. segmentedControl addTarget: self action: @ selector (segmentedControlAction :) forControlEvents: UIControlEventValueChanged]; self. segmentedControl. selectedSegmentIndex = 0; // create scrollView self. scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake (0, 64, [UIScreen mainScreen]. bounds. size. width, [UIScreen mainScreen]. bounds. size. height-64)]; [self. view addSubview: self. scrollView]; // set the content of scrollView self. scrollView. contentSize = CGSizeMake ([UIScreen mainScreen]. bounds. size. width * 2, [UIScreen mainScreen]. bounds. size. height-64); self. scrollView. pagingEnabled = YES; self. scrollView. bounces = NO; // create the controller self. firstVC = [FirstViewController new]; self. secondTVC = [[SecondTableViewController alloc] initWithStyle: UITableViewStylePlain]; // Add the subcontroller as self [self addChildViewController: self. firstVC]; [self addChildViewController: self. secondTVC]; self. firstVC. view. frame = CGRectMake (0, 0, self. scrollView. frame. size. width, CGRectGetHeight (self. scrollView. frame); self. secondTVC. view. frame = CGRectMake ([UIScreen mainScreen]. bounds. size. width, 0, self. scrollView. frame. size. width, CGRectGetHeight (self. scrollView. frame); [self. scrollView addSubview: self. firstVC. view]; [self. scrollView addSubview: self. secondTVC. view]; // sets the proxy self of scrollView. scrollView. delegate = self ;}
Segment controller click Method
- (void)segmentedControlAction:(UISegmentedControl *)sender{ [self.scrollView setContentOffset:CGPointMake(sender.selectedSegmentIndex * self.scrollView.frame.size.width, 0) animated:NO];}- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSInteger n = scrollView.contentOffset.x / scrollView.frame.size.width; self.segmentedControl.selectedSegmentIndex = n;}
First/and second are UIViewController and UITableViewController. You can set only the color to see the effect (this is not created here)
Final effect: