鎮(zhèn)樓圖
為iPad或者iPhone大屏幕進行適配分屏顯示,蘋果提供了UISplitViewController
這個容器熟丸。
關(guān)鍵參數(shù)介紹
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]];
// 內(nèi)容顯示控制器
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) {
// 點擊直接修改顯示內(nèi)容
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跳轉(zhuǎn)
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
內(nèi)容控制器中
#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顯示好看芋哭,分屏?xí)r隱藏了內(nèi)容控制器的導(dǎo)航欄
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