Use segment controller and UIScrollView in iOS

Source: Internet
Author: User

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:

 

 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.