昨天看陈一发儿(对,没错,就是那个穿的最多,开车最快的发姐,😂😂
)的录播时,想到了iPad
版本的直播软件框架,和UITabbarController + UINavigationController
的通用框架模式不一样,这里使用UISplitViewController
来实现相应的框架模式,先看一张效果图
下面开始上代码,很简单的
在AppDelegate中的设置
#import "AppDelegate.h"
#import "MasterViewController.h"
#import "DetailsViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#define Width [UIScreen mainScreen].bounds.size.width
@interface AppDelegate ()
@property (nonatomic,strong) UINavigationController *navigationControllerDetails;
@property (nonatomic,strong) UINavigationController *navigationControllerFirst;
@property (nonatomic,strong) UINavigationController *navigationControllerSecond;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[self setupSpliteViewController];
return YES;
}
#pragma mark 设置SpliteViewController
- (void)setupSpliteViewController
{
//左侧主视图
MasterViewController *master = [MasterViewController new];
//右侧默认视图
DetailsViewController *details = [DetailsViewController new];//刚进入应用默认显示的Controller
self.navigationControllerDetails = [[UINavigationController alloc]initWithRootViewController:details];
FirstViewController *first = [FirstViewController new];
self.navigationControllerFirst = [[UINavigationController alloc]initWithRootViewController:first];
SecondViewController *second = [SecondViewController new];
self.navigationControllerSecond = [[UINavigationController alloc]initWithRootViewController:second];
self.spliteViewController = [[UISplitViewController alloc]init];
self.spliteViewController.viewControllers = @[master,self.navigationControllerDetails,self.navigationControllerFirst,self.navigationControllerSecond];
self.spliteViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;//设置左侧主视图Master Controller的显示模式,现在是一直显示。如果设置为横屏显示竖屏不显示,还可以再设置一下相关的手势属性,如presentsWithGesture
self.spliteViewController.maximumPrimaryColumnWidth = 128.0f;//调整左侧主视图Master Controller的最大显示宽度
self.window.rootViewController = self.spliteViewController;
}
说明
- DetailsViewController是默认显示的视图控制器
- MasterViewController是左侧的管理视图控制器。我们可以在这里面做一些操作,比如切换控制器,自定义一些视图。
如下代码,在MasterViewController中
#import "MasterViewController.h"
@interface MasterViewController ()
@property (nonatomic,strong) UINavigationController *navigationControllerDetails;
@property (nonatomic,copy) NSArray *dataSource;
@end
@implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@",self.splitViewController.viewControllers);
self.dataSource = self.splitViewController.viewControllers;
}
- (IBAction)changeViewController:(id)sender {
UIButton *btn = (UIButton *)sender;
[self.splitViewController showViewController:self.dataSource[btn.tag] sender:nil];
}
几乎所有的代码都在上面了,你们可以尝试自己写一下demo。当然,除了我的这种写法方式,肯定还会有别的更好的,希望各位能多多指教,谢谢了。
网友评论