Ответ 1
instead spv.delegate=nil; spv.delegate=self;
вам нужно сделать следующее:
[spv willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
В моем XCode 4.2 я создал новый проект с разбитым представлением.
Затем в DetailViewController.m
файле я добавлю этот метод
- (BOOL)splitViewController: (UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
//This method is only available in iOS5
return NO;
}
Теперь, делая это, я могу одновременно показывать как левую, так и правую часть моего контроллера splitview.
Теперь я добавил UIBarButtonItem
в моей панели навигации DetailViewController
, и я хочу, используя, с помощью которого я могу скрыть и показать мой главный вид как в режиме портрета, так и в ландшафтном режиме.
- (IBAction)hideUnhide:(id)sender
{
//How can hide & unhide
}
Как я могу это сделать?
instead spv.delegate=nil; spv.delegate=self;
вам нужно сделать следующее:
[spv willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
'setNeedsLayout' заставляет UISplitViewController запрашивать "shouldHideViewController"
- (IBAction)hideUnhide:(id)sender {
UISplitViewController* spv = ...;
self.hideMaster= !self.hideMaster;
[ spv.view setNeedsLayout ]
}
В iOS 5.1 вы должны сделать это следующим образом:
Внутри DetailViewController.m
- (IBAction)hideUnhide:(id)sender {
UISplitViewController* spv = ...;
self.hideMaster= !self.hideMaster;
[spv.view setNeedsLayout];
spv.delegate = nil;
spv.delegate = self;
}
- (BOOL)splitViewController:(UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation {
return self.hideMaster;
}
Я объединил вышеупомянутые ответы, и в IOS 6 хорошо работает следующее:
// In split delegate
-(void)hideMaster:(BOOL)hideState
{
_masterIsHidden = hideState;
[self.splitViewController.view setNeedsLayout];
self.splitViewController.delegate = nil;
self.splitViewController.delegate = self;
[self.splitViewController willRotateToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:0];
}
-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
return self.masterIsHidden;
}
-(IBAction)clickToShowMaster:(id)sender
{
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"prev.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToHidemaster:)];
self.navigationItem.leftBarButtonItem = systemItem1;
[self.tabBarController.tabBar setHidden:NO];
[self hideMaster:NO];
}
-(void)hideMaster:(BOOL)hideState
{
ishideMaster=hideState;
[self.splitViewController.view setNeedsLayout];
self.splitViewController.delegate = nil;
self.splitViewController.delegate = self;
[self.splitViewController willRotateToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:0];
}
-(void)hideMaster:(BOOL)hideState
{
ishideMaster=hideState;
[self.splitViewController.view setNeedsLayout];
self.splitViewController.delegate = nil;
self.splitViewController.delegate = self;
[self.splitViewController willRotateToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:0];
}
#pragma mark - Split view
-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
if(UIInterfaceOrientationIsPortrait(orientation))
{
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"down.png"] style:UIBarButtonItemStylePlain target:self action:@selector(showPopup)];
self.navigationItem.leftBarButtonItem = systemItem1;
[self setUIforPortrait];
return YES;
}
if (UIInterfaceOrientationIsLandscape(orientation))
{
if(ishideMaster==TRUE)
{
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"next.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToShowMaster:)];
self.navigationItem.leftBarButtonItem = systemItem1;
[self setUIForFullLandscape];
}
else
{
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"prev.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToHidemaster:)];
self.navigationItem.leftBarButtonItem = systemItem1;
[self setUIForHalfLandscape];
}
return ishideMaster;
}
}
//add the navigation button on left top, to pop-up master view.
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
[barButtonItem setImage:[UIImage imageNamed:@"down.png"]];
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"down.png"] style:UIBarButtonItemStylePlain target:self action:@selector(showPopup)];
self.navigationItem.leftBarButtonItem = systemItem1;
self.masterPopoverController = popoverController;
self.masterPopoverController.delegate=self;
}
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
// Called when the view is shown again in the split view, invalidating the button and popover controller.
//;
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
if(ishideMaster==FALSE)
{
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"prev.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToHidemaster:)];
self.navigationItem.leftBarButtonItem = systemItem1;
}
else
{
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"next.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToShowMaster:)];
self.navigationItem.leftBarButtonItem = systemItem1;
}
}
else if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
}
//self.masterPopoverController = nil;
}
Что касается комментария wzbozon о необходимости переназначить делегата, я обнаружил, что две строки
self.splitViewController.delegate = nil;
self.splitViewController.delegate = self;
... не нужны на симуляторе, но были необходимы на моем iOS5 iPad 1. Без них поведение hide/show не произошло (нажатие кнопки не сворачивало главное представление).
В iOS8 это легко.
Чтобы скрыть его
[UIView animateWithDuration:0.2 animations:^{
splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
} completion:nil];
Чтобы показать это
[UIView animateWithDuration:0.2 animations:^{
self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
} completion:nil];
Ну, легкая часть вашего вопроса - использовать bool, скажем, свойство hideMaster, а затем
- (IBAction)hideUnhide:(id)sender
{
self.hideMaster= !self.hideMaster;
}
а затем...
- (BOOL)splitViewController: (UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
//This method is only available in iOS5
return self.hideMaster;
}
Это работает отлично, но shouldHideViewController вызывается только во время перерисовки splitVC, например, во время вращения, поэтому мастер только скрывает/скрывает.
Вы можете показать/скрыть главный ViewController, вызвав действие свойства displayModeButtonItem
UISplitViewController
:
Свифта
if let displayModeButtonItem = splitViewController?.displayModeButtonItem() {
displayModeButtonItem.target?.performSelector(displayModeButtonItem.action)
}
Objective-C
UIBarButtonItem *displayModeButtonItem = [self.splitViewController displayModeButtonItem];
[displayModeButtonItem.target performSelector: displayModeButtonItem.action];
Мне кажется более подходящим для меня, чем вмешиваться с делегатом, ориентацией и макетом в то же время.
SWIFT 3.0
Я использовал
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let object = self.exercises[indexPath.row]
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
UIView.animate(withDuration: 0.2, delay: 0.0, options: [.curveEaseOut], animations: {
self.splitViewController?.preferredDisplayMode = .primaryHidden
}, completion: nil)
}
}
}
- (BOOL)splitViewController:(UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation {
[spv.view setNeedsLayout]; //add the line
return self.hideMaster;
}