@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UISplitViewController *splitVC = [[UISplitViewControlleralloc]init];
MasterViewController *masterVC = [[MasterViewControlleralloc]init];
UINavigationController *masterNAV = [[UINavigationControlleralloc]initWithRootViewController:masterVC];
DetailViewController *detailVC = [[DetailViewControlleralloc]init];
UINavigationController *detailNAV = [[UINavigationControlleralloc]initWithRootViewController:detailVC];
splitVC.viewControllers = @[masterNAV,detailNAV];
self.window.rootViewController = splitVC;
self.window.backgroundColor = [UIColorwhiteColor];
splitVC.delegate = self;
//是否通过手势控制浮动显示master窗口,默认为YES,窗口可拉出;NO,窗口不可拉出
splitVC.presentsWithGesture =YES;
return YES;
}
//隐藏master主窗口
- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc {
NSLog(@"隐藏主窗口");
}
// 显示master主窗口
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
NSLog(@"显示主窗口");
}
// master主窗口作为浮动窗口显示
- (void)splitViewController:(UISplitViewController *)svc popoverController:(UIPopoverController *)pc willPresentViewController:(UIViewController *)aViewController{
NSLog(@"浮动主窗口");
}
//如果协议方法不实现,横屏显示master窗口,竖屏隐藏master窗口
//返回YES:隐藏master主窗口;NO:显示master主窗口
//- (BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation{
// return YES;
//}
@implementation MasterViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.title =@"主窗口";
self.view.backgroundColor = [UIColorredColor];
}
@implementation DetailViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.title =@"详情窗口";
self.view.backgroundColor = [UIColoryellowColor];
UIBarButtonItem *leftBtn = [[UIBarButtonItemalloc]initWithTitle:@"pop窗口"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(clickAction:)];
self.navigationItem.leftBarButtonItem = leftBtn;
PopTableViewController *popTVC = [[PopTableViewControlleralloc]init];
//如果浮云窗口需要导航栏,即创建导航控制器
UINavigationController *popNAV = [[UINavigationControlleralloc]initWithRootViewController:popTVC];
//创建浮动窗口,UIPopoverController继承于NSObject,不是视图控制器
_popCtrl = [[UIPopoverControlleralloc]initWithContentViewController:popNAV]; //需将视图控制器放入pop窗口中
//设置浮云窗口大小
_popCtrl.popoverContentSize =CGSizeMake(320,500);
UIBarButtonItem *rightBtn = [[UIBarButtonItemalloc]initWithTitle:@"模态窗口"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(showModal:)];
self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void)clickAction:(UIBarButtonItem *)btnItem{
//判断pop窗口是显示还是隐藏
if (_popCtrl.popoverVisible) {
[_popCtrldismissPopoverAnimated:YES];//如果显示,让其隐藏
}else {
[_popCtrlpresentPopoverFromBarButtonItem:btnItem permittedArrowDirections:UIPopoverArrowDirectionAnyanimated:YES];// UIPopoverArrowDirectionAny 任何方向
}
}
-(void)showModal:(UIBarButtonItem *)btnItem {
UIViewController *modalVC = [[UIViewControlleralloc]init];
modalVC.title = @"模态窗口";
UINavigationController *modalNAV = [[UINavigationControlleralloc]initWithRootViewController:modalVC];
modalNAV.modalPresentationStyle =UIModalPresentationFormSheet;
/*UIModalPresentationFullScreen = 0, 默认全屏
UIModalPresentationPageSheet 竖屏时,全屏显示,横屏时,中间显示
UIModalPresentationFormSheet 在屏幕中心显示
UIModalPresentationCurrentContext 在触发的控制器上显示 */
[selfpresentViewController:modalNAV animated:YEScompletion:NULL];
}
@implementation PopTableViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.title =@"浮动窗口";
self.view.backgroundColor = [UIColororangeColor];
}