iPad开发中UISplitViewController和UIPopoverPresentationController的使用

对于 iPad 开发来说,因为屏幕大,为了用户操作的方便,所以通常使用 UISplitViewController来实现导航。
同时也会使用UIPopoverPresentationController实现浮窗功能。

源码地址

效果图
效果图

代码示例:
设置根视图控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    //
    SpliteViewController *splitViewController = [[SpliteViewController alloc] init];
    self.window.rootViewController = splitViewController;
    //
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;
}

SpliteViewController视图控制器继承UISplitViewController,并设置为app的根视图控制器。

#import <UIKit/UIKit.h>

@interface SpliteViewController : UISplitViewController

@end
#import "SpliteViewController.h"
#import "MainViewController.h"
#import "BeiChiViewController.h"
#import "CRVViewController.h"
#import "XianDaiViewController.h"
#import "LeiKeSaSiViewController.h"


@interface SpliteViewController () <UISplitViewControllerDelegate>

@end

@implementation SpliteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setSpliteController];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setSpliteController
{
    //
    CGFloat originXY = 180.0f;
    //
    MainViewController *menuVC = [[MainViewController alloc] init];
    UINavigationController *menuNav = [[UINavigationController alloc] initWithRootViewController:menuVC];
    BeiChiViewController *beiChiVC = [[BeiChiViewController alloc] init];
    UINavigationController *BeiChiNav = [[UINavigationController alloc] initWithRootViewController:beiChiVC];
    CRVViewController *crvVC = [[CRVViewController alloc] init];
    UINavigationController *crvNav = [[UINavigationController alloc] initWithRootViewController:crvVC];
    XianDaiViewController *xianDaiVC = [[XianDaiViewController alloc] init];
    UINavigationController *xianDaiNav = [[UINavigationController alloc] initWithRootViewController:xianDaiVC];
    LeiKeSaSiViewController *leiKeSaSiVC = [[LeiKeSaSiViewController alloc] init];
    UINavigationController *leiKeSaSiNav = [[UINavigationController alloc] initWithRootViewController:leiKeSaSiVC];
    //
    self.delegate = self;
    self.minimumPrimaryColumnWidth = originXY;
    self.maximumPrimaryColumnWidth = originXY;
    self.view.backgroundColor = [UIColor orangeColor];
    /*
     UISplitViewControllerDisplayModeAutomatic,
     UISplitViewControllerDisplayModePrimaryHidden,
     UISplitViewControllerDisplayModeAllVisible,
     UISplitViewControllerDisplayModePrimaryOverlay,
    */
    self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
    //
    SpliteViewController __weak *weakSelf = self;
    menuVC.itemClick = ^(NSInteger index) {
        UINavigationController *nextController = controllers[index + 1];
        [weakSelf showDetailViewController:nextController sender:nil];
    };
}

@end

UIPopoverPresentationController浮窗的使用

- (void)itemClick
{
    // 1 设置内容控制器
    MenuViewController *controller = [[MenuViewController alloc] init];
    controller.preferredContentSize = CGSizeMake(200, 200);
    controller.modalPresentationStyle = UIModalPresentationPopover;

    // 2 出现
    // 2-1 出现在UIBarButtonItem上面的
    UIPopoverPresentationController *popController = [controller popoverPresentationController];
    popController.barButtonItem = self.navigationItem.rightBarButtonItem;

    // 3 显示
    [self presentViewController:controller animated:YES completion:nil];

    // 4
    controller.itemClick = ^(NSInteger index) {

        // 5 消失
        [self dismissViewControllerAnimated:YES completion:NULL];

        // 6
        MenuViewController *nextVC = [MenuViewController new];
        [self.navigationController pushViewController:nextVC animated:YES];
    };
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值