如何使用splitViewController何时显示barButtonItem:willChangeToDisplayMode:在iOS8上

我的应用程序的结构,很喜欢这样的:如何使用splitViewController何时显示barButtonItem:willChangeToDisplayMode:在iOS8上

UISplitViewController: 

the master:NavigationController1->UITableViewController

the detail:NavigationController2->UIWebViewController

我想显示barButtonItem当视图那张肖像模式在iPad上 ,我知道如何实现它在通过iOS7 willHideViewController:

-(void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc{ 

barButtonItem.title = @"Course";

self.navigationItem.leftBarButtonItem = barButtonItem;

}

-(void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{

if (barButtonItem == self.navigationItem.leftBarButtonItem) {

self.navigationItem.leftBarButtonItem = nil;

}

}

然而,该方法是在IOS 8弃用,我试图使用:

-(void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode{ 

if (displayMode == UISplitViewControllerDisplayModePrimaryHidden) {

self.navigationItem.leftBarButtonItem = svc.displayModeButtonItem;

}else{

self.navigationItem.leftBarButtonItem = nil;

}

}

此方法仅适用于显示模式更改时,但不适用于应用程序首次以纵向方向启动时。 那么如何在第一次以纵向方向加载应用程序时显示barButtonItem。

回答:

当您的视图控制器显示出来,您可以添加栏按钮:

- (void)viewWillAppear:(BOOL)animated 

{

[super viewWillAppear:animated];

if (self.splitViewController.displayMode == UISplitViewControllerDisplayModePrimaryHidden)

{

UIBarButtonItem *barButtonItem = self.splitViewController.displayModeButtonItem;

barButtonItem.title = @"Show master";

self.navigationItem.leftBarButtonItem = barButtonItem;

}

}

这只会增加该按钮时,主目前处于隐藏状态。

回答:

-(void)viewWillAppear:(BOOL)animated{ 

[super viewWillAppear:animated];

if (self.splitViewController.displayMode == UISplitViewControllerDisplayModePrimaryHidden){

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Course" style:self.splitViewController.displayModeButtonItem.style target:self.splitViewController.displayModeButtonItem.target action:self.splitViewController.displayModeButtonItem.action];

}

}

-(void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode{

if (displayMode == UISplitViewControllerDisplayModePrimaryHidden) {

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Course" style:svc.displayModeButtonItem.style target:svc.displayModeButtonItem.target action:svc.displayModeButtonItem.action];

}else{

self.navigationItem.leftBarButtonItem = nil;

}

}

以上是 如何使用splitViewController何时显示barButtonItem:willChangeToDisplayMode:在iOS8上 的全部内容, 来源链接: utcz.com/qa/257814.html

回到顶部