iPad专有api:漂浮视图控制器UIPopoverViewController和分割视图控制器UISplitViewController
----------------------------------UIPopoverViewController----------------------------------
AppDelegate.m
UINavigationController *navi; if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad) {//Ipad设备 navi = [[UINavigationController alloc]initWithRootViewController:[IpadViewController new]]; } else { navi = [[UINavigationController alloc]initWithRootViewController:[IphoneViewController new]]; } self.window.rootViewController = navi;
IpadViewController.m
#import "IpadViewController.h" #import "ListViewController.h" @interface IpadViewController () { UIPopoverController *_leftPopController; } @end @implementation IpadViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"Ipad"; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Pop" style:UIBarButtonItemStyleDone target:self action:@selector(jumpLeftPop:)]; } -(void)jumpLeftPop:(id)sender { if (_leftPopController == nil) { ListViewController *list = [ListViewController new]; UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:list]; //漂浮视图控制器 _leftPopController = [[UIPopoverController alloc]initWithContentViewController:nav]; _leftPopController.delegate = self; _leftPopController.popoverContentSize = CGSizeMake(320, 300); list.pop = _leftPopController; } [_leftPopController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];//模态显示 }
ListViewController.m
#import "ListViewController.h" @interface ListViewController () @end @implementation ListViewController - (void)viewDidLoad { self.title = @"选择你喜欢的颜色"; _listTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; _listTableView.delegate = self; _listTableView.dataSource = self; [self.view addSubview:_listTableView]; [super viewDidLoad]; // Do any additional setup after loading the view. self.dataSourse = [[NSMutableArray alloc]initWithArray: @[@"red",@"yellow",@"blue",@"green",@"gray"]]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSourse.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"cellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } cell.textLabel.text = [self.dataSourse objectAtIndex:indexPath.row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.pop dismissPopoverAnimated:YES]; }
-----------------------------------------SliptViewControllerDemo-----------------------------------------
AppDelegate.m
MasterViewController *master = [[MasterViewController alloc]init]; UINavigationController *navMaster = [[UINavigationController alloc]initWithRootViewController:master]; DetailViewController *detail = [[DetailViewController alloc]init]; UINavigationController *navDetail = [[UINavigationController alloc]initWithRootViewController:detail]; //分割视图控制器 UISplitViewController *split = [[UISplitViewController alloc]init]; split.viewControllers = @[navMaster,navDetail]; split.delegate = detail; split.tabBarItem = detail.tabBarItem; self.window.rootViewController = split;
DetailViewController.h
@interface DetailViewController : UIViewController<UIPopoverControllerDelegate,UISplitViewControllerDelegate> @property(nonatomic,retain)UIPopoverController *pop; @end
DetailViewController.m
#import "DetailViewController.h" #import "ModelViewController.h" @interface DetailViewController () { ModelViewController *_modelController; UISegmentedControl *_segment; } @end @implementation DetailViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"detail"; self.view.backgroundColor = [UIColor whiteColor]; _segment = [[UISegmentedControl alloc]initWithItems:@[@"one",@"two",@"three",@"four"]]; _segment.frame = CGRectMake(350,100, 320, 40); _segment.selectedSegmentIndex = 0; [self.view addSubview:_segment]; UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; nextBtn.frame = CGRectMake(350, 200, 200, 40); [nextBtn setTitle:@"nextController" forState:0]; [nextBtn addTarget:self action:@selector(nextContrller:) forControlEvents:7]; [self.view addSubview:nextBtn]; } -(void)nextContrller:(id)sender { if (_modelController==nil) { _modelController = [[ModelViewController alloc]init]; } _modelController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;//转出动画样式 switch (_segment.selectedSegmentIndex) { case 0: { _modelController.modalPresentationStyle = UIModalPresentationFullScreen;//全屏,默认样式,IPhone只能全屏 break; } case 1: { _modelController.modalPresentationStyle = UIModalPresentationPageSheet;//竖屏全屏,横屏宽768 break; } case 2: { _modelController.modalPresentationStyle = UIModalPresentationFormSheet;//不是全屏的,固定540*620 break; } case 3: { _modelController.modalPresentationStyle = UIModalPresentationCurrentContext;//detailController控制器自身显示大小有关,全屏自身控制器 break; } default: break; } [self presentViewController:_modelController animated:YES completion:nil]; } #pragma mark 分割视图控制器DelegateMethods - (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc { barButtonItem.title = @"主页列表"; self.navigationItem.leftBarButtonItem = barButtonItem; self.pop = pc; } - (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem { self.navigationItem.leftBarButtonItem = nil; self.pop = nil; } @end
MasterViewController.h
@interface MasterViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property(nonatomic,retain)NSMutableArray *dataSourse; @property(nonatomic,retain)UITableView *listTableView; @end
MasterViewController.m
#import "MasterViewController.h" @interface MasterViewController () @end @implementation MasterViewController - (void)viewDidLoad { self.title = @"master"; _listTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; _listTableView.delegate = self; _listTableView.dataSource = self; [self.view addSubview:_listTableView]; [super viewDidLoad]; self.dataSourse = [[NSMutableArray alloc]initWithArray: @[@"red",@"yellow",@"blue",@"green",@"gray"]]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSourse.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"cellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } cell.textLabel.text = [self.dataSourse objectAtIndex:indexPath.row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } @end
Demo下载:https://github.com/forrHuen/iPadDemo