AppDelegate.h
//默认
AppDelegate.m
// AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *vc=[[ViewController alloc]init];
vc.title=@"分段控件练习";
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController=nav;
return YES;
}
ViewController.h
//默认
ViewController.m
#import "ViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) FirstViewController *firstVC;
@property (nonatomic, strong) SecondViewController *secondVC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 适应scrollView
self.automaticallyAdjustsScrollViewInsets = NO;
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"你好", @"我的"]];
self.navigationItem.titleView = self.segmentedControl;
[self.segmentedControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
self.segmentedControl.selectedSegmentIndex = 0;
// 创建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];
// 设置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;
// 创建控制器
self.firstVC = [[FirstViewController alloc]init];
self.secondVC = [[SecondViewController alloc]init];
// 添加为self的子控制器
[self addChildViewController:self.firstVC];
[self addChildViewController:self.secondVC];
self.firstVC.view.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));
self.secondVC.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.secondVC.view];
// 设置scrollView的代理
self.scrollView.delegate = self;
}
- (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;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}