在采用新的UISplitViewController
时,我尝试将更改作为使用UISplitViewControllerDisplayModeAutomatic
模式时发生的默认行为。
当以纵向工作时,我希望当用户触发推送到细节侧时隐藏主要叠加层。默认情况下,覆盖图保持在屏幕上,直到用户点击详细信息一侧。
我已尝试将以下内容与代理人一起使用:
- (BOOL)splitViewController:(UISplitViewController *)splitViewController showDetailViewController:(UIViewController *)vc sender:(id)sender
{
if (splitViewController.displayMode == UISplitViewControllerDisplayModePrimaryOverlay) {
[UIView animateWithDuration:0.3 animations:^{
splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
}];
}
return NO;
}
这为我提供了所需的纵向行为,但这打破了横向模式(我想表现得像UISplitViewControllerDisplayModeAllVisible
)。如果您已完成推动然后旋转设备,则左侧仍然是隐藏的(如预期的那样)。我无法找到一个appriprite地方来重新设置旋转时的首选模式以显示左侧(因为特征集合不能用于告诉iPad上的横向与纵向)。
如何手动触发重叠的解雇?
Dupe note: iOS8 完全改变了UISplitViewController
,所以在2014年6月之前所有其他的SO答案都可能是错误的(我已经挖过许多他们,只是因为)
答案 0 :(得分:6)
我遇到了和你一样的问题。我在Xamarin的单声道触控平台上这样做,但我认为结果会是一样的。
与LaborEtArs所说的一样,将代码移动到主视图控制器的prepareForSegue:sender:
方法。然后在将模式设置为隐藏后将模式设置为自动:
if (splitViewController.displayMode == UISplitViewControllerDisplayModePrimaryOverlay) {
splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAutomatic;
}
以这种方式完成后,它不再破坏横向模式。
答案 1 :(得分:3)
只需将代码(不含return NO;
)放在主视图控制器prepareForSegue:sender:
或tableView:didSelectRowAtIndexPath:
方法中。
它完美无缺!
答案 2 :(得分:2)
除了LaborEtArs在prepareForSegue:sender:
或tableView:didSelectRowAtIndexPath:
中制作动画的建议外,如果您的应用通常将splitViewController:preferredDisplayMode
设置为UISplitViewControllerDisplayModeAutomatic
,则只需使用animateWithDuration:
方法和完成处理程序在动画后恢复displayMode:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
// configure the detail controller
if (self.splitViewController.displayMode == UISplitViewControllerDisplayModePrimaryOverlay) {
[UIView animateWithDuration:0.3 animations:^{
self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
} completion:^(BOOL finished){
self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAutomatic;
}];
}
}
答案 3 :(得分:1)
使用拆分视图委托可以轻松实现,如下所示:
@interface AppDelegate ()
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
@property (nonatomic, assign) UIPopoverController *splitPopoverController;
#pragma clang diagnostic pop
@end
@implementation AppDelegate
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)splitViewController:(UISplitViewController *)splitViewController popoverController:(UIPopoverController *)popoverController willPresentViewController:(UIViewController *)vc{
#pragma clang diagnostic pop
self.splitPopoverController = popoverController;
}
- (BOOL)splitViewController:(UISplitViewController *)splitViewController showDetailViewController:(UIViewController *)vc sender:(nullable id)sender{
[self.splitPopoverController dismissPopoverAnimated:YES];
return NO;
}
@end
答案 4 :(得分:0)
这是Swift版本:
if (self.splitViewController?.displayMode == UISplitViewControllerDisplayMode.PrimaryOverlay){
splitViewController?.preferredDisplayMode = UISplitViewControllerDisplayMode.PrimaryHidden
splitViewController?.preferredDisplayMode = UISplitViewControllerDisplayMode.Automatic
} else {
println(self.splitViewController?.displayMode)
}
放在prepareForSegue
中答案 5 :(得分:-1)
@implementation SplitProductView
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
}
- (void)viewWillAppear:(BOOL)animated{
[self resetSplit:[[UIApplication sharedApplication] statusBarOrientation]];
[super viewWillAppear:animated];
}
-(void)resetSplit :(UIInterfaceOrientation)toInterfaceOrientation {
//TODOX:iphone
if (isPad)
{
if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
self.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
}
else{
//if (self.displayMode == UISplitViewControllerDisplayModePrimaryOverlay)
{
self.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
self.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
self.preferredDisplayMode = UISplitViewControllerDisplayModeAutomatic;
}
}
}
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (isPad)
{
if (!UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])){
self.preferredDisplayMode =UISplitViewControllerDisplayModePrimaryOverlay;
}
}
[self resetSplit:toInterfaceOrientation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)splitViewController:(UISplitViewController *)splitViewController
collapseSecondaryViewController:(UIViewController *)secondaryViewController
ontoPrimaryViewController:(UIViewController *)primaryViewController {
return YES;
}