UISplitViewController做大屏幕分屏顯示

鎮(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

示例圖

示例Demo下載

Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末咖气,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子惭缰,更是在濱河造成了極大的恐慌,老刑警劉巖笼才,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件漱受,死亡現(xiàn)場離奇詭異,居然都是意外死亡骡送,警方通過查閱死者的電腦和手機昂羡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來摔踱,“玉大人虐先,你說我怎么就攤上這事∨煞螅” “怎么了蛹批?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長篮愉。 經(jīng)常有香客問我般眉,道長,這世上最難降的妖魔是什么潜支? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任甸赃,我火速辦了婚禮,結(jié)果婚禮上冗酿,老公的妹妹穿的比我還像新娘埠对。我一直安慰自己,他們只是感情好裁替,可當(dāng)我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布项玛。 她就那樣靜靜地躺著,像睡著了一般弱判。 火紅的嫁衣襯著肌膚如雪襟沮。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天,我揣著相機與錄音开伏,去河邊找鬼膀跌。 笑死,一個胖子當(dāng)著我的面吹牛固灵,可吹牛的內(nèi)容都是我干的捅伤。 我是一名探鬼主播,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼巫玻,長吁一口氣:“原來是場噩夢啊……” “哼丛忆!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起仍秤,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤熄诡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后诗力,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體粮彤,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年姜骡,在試婚紗的時候發(fā)現(xiàn)自己被綠了导坟。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡圈澈,死狀恐怖惫周,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情康栈,我是刑警寧澤递递,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站啥么,受9級特大地震影響登舞,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜悬荣,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一菠秒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧氯迂,春花似錦践叠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至轿曙,卻和暖如春弄捕,著一層夾襖步出監(jiān)牢的瞬間僻孝,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工守谓, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留穿铆,地道東北人。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓分飞,卻偏偏與公主長得像悴务,于是被迫代替她去往敵國和親睹限。 傳聞我的和親對象是個殘疾皇子譬猫,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容