
为iPad或者iPhone大屏幕进行适配分屏显示,苹果提供了UISplitViewController
这个容器。
关键参数介绍
preferredDisplayMode
UISplitViewControllerDisplayModeAutomatic
, 自动,默认样式
UISplitViewControllerDisplayModePrimaryHidden
, 主视图隐藏 横竖屏主视图都会隐藏,可以通过手势来控制主视图的显隐
UISplitViewControllerDisplayModeAllVisible
, 始终显示 横竖屏主视图都会显示,不可以通过手势来控制主视图的显隐
UISplitViewControllerDisplayModePrimaryOverlay
, 主视图悬停 横竖屏主视图都会显示,可以通过手势来控制主视图的显隐
-
preferredPrimaryColumnWidthFraction
主视图的宽度比例:主视图的宽度比例 = 主视图宽度 / SplitViewController整体宽度
-
maximumPrimaryColumnWidth
主视图的宽度最大值 -
minimumPrimaryColumnWidth
主视图的宽度最小值
主视图的宽度比例不好控制,并且受最大值
maximumPrimaryColumnWidth
和最小值minimumPrimaryColumnWidth
两个属性限制
简单实用
在AppDelegate
中初始化代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UISplitViewController *vc = [[UISplitViewController alloc] init];
vc.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
// 主控制
UINavigationController *master = [[UINavigationController alloc] initWithRootViewController:[[MasterViewController alloc] init]];
// 内容显示控制器
UINavigationController *content = [[UINavigationController alloc] initWithRootViewController:[[ContentViewController alloc] init]];
vc.viewControllers = @[master, content];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
主控制器中
#import "MasterViewController.h"
#import "ContentViewController.h"
@interface MasterViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"主控制器";
[self.view addSubview:self.tableView];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.tableView.frame = self.view.bounds;
}
#pragma mark - UITableView DataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
return cell;
}
#pragma mark - UITableView Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// 小屏幕不支持分屏
BOOL isLandscape = (UIApplication.sharedApplication.statusBarOrientation == UIInterfaceOrientationLandscapeLeft || UIApplication.sharedApplication.statusBarOrientation == UIInterfaceOrientationLandscapeRight);
if (isLandscape && UIScreen.mainScreen.bounds.size.height > 375) {
// 点击直接修改显示内容
UINavigationController *nav = self.splitViewController.viewControllers.lastObject;
if (nav.viewControllers.count > 0) {
[nav.viewControllers.lastObject.navigationController popToRootViewControllerAnimated:YES];
}
ContentViewController *vc = nav.viewControllers.firstObject;
vc.contentText = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
} else {
// 点击进行push跳转
ContentViewController *vc = [[ContentViewController alloc] init];
vc.contentText = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
[self.navigationController pushViewController:vc animated:YES];
}
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.tableFooterView = [[UIView alloc] init];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}
return _tableView;
}
@end
内容控制器中
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface ContentViewController : UIViewController
@property (nonatomic, copy) NSString *contentText;
@end
NS_ASSUME_NONNULL_END
#import "ContentViewController.h"
#import "OtherViewController.h"
@interface ContentViewController ()<UINavigationControllerDelegate>
@property (nonatomic, strong) UILabel *textLabel;
@end
@implementation ContentViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.delegate = self;
self.textLabel = [[UILabel alloc] init];
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.textAlignment = NSTextAlignmentCenter;
self.textLabel.font = [UIFont systemFontOfSize:100 weight:UIFontWeightMedium];
[self.view addSubview:self.textLabel];
self.textLabel.text = self.contentText.length > 0 ? self.contentText : @"1";
self.view.backgroundColor = [UIColor orangeColor];
}
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated {
BOOL isLandscape = (UIApplication.sharedApplication.statusBarOrientation == UIInterfaceOrientationLandscapeLeft ||
UIApplication.sharedApplication.statusBarOrientation == UIInterfaceOrientationLandscapeRight);
// 为了Demo显示好看,分屏时隐藏了内容控制器的导航栏
if (isLandscape && UIScreen.mainScreen.bounds.size.height > 375) {
BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
[self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
} else {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
OtherViewController *vc = [[OtherViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
- (void)setContentText:(NSString *)contentText {
_contentText = contentText;
self.textLabel.text = contentText;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.textLabel.frame = self.view.bounds;
}
@end
示例图

网友评论