【问题标题】:Simulate Split View Controller's Display Mode Button Item in Code在代码中模拟拆分视图控制器的显示模式按钮项
【发布时间】:2014-12-09 07:21:29
【问题描述】:

拆分视图控制器提供了一个显示模式按钮项,如下所示:

隐藏主视图并展开详细视图。

我的问题是,有没有办法在代码中模拟这个按钮的动作?

【问题讨论】:

  • 改变displayMode(或preferredDisplayMode)会改变没有动画的显示。我希望它是动画的。
  • 这里有同样的问题。你找到答案了吗?
  • 我最终不需要这个,所以不......但我认为将 setPreferredDisplayMode 放在动画块中就可以了。
  • 这里也一样。结果我根本没有使用 UISplitViewController。相反,AMSlideMenu 具有我需要的灵活性。谢谢老兄。

标签: ios objective-c cocoa-touch swift


【解决方案1】:

同样,我也在寻找这个问题的答案。遗憾的是,这方面的发现并不多。

一个简单的方法是使用UISplitViewController Delegate:

-(UISplitViewControllerDisplayMode)targetDisplayModeForActionInSplitViewController:(UISplitViewController *)svc{

创建我们想要的按钮并使用@Selector处理操作

下面的代码可能会有所帮助:

-(UISplitViewControllerDisplayMode)targetDisplayModeForActionInSplitViewController:(UISplitViewController *)svc{
if (svc.displayMode == UISplitViewControllerDisplayModePrimaryHidden) {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStylePlain target:self action:@selector(showMenu)];
}else{
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
        self.navigationItem.leftBarButtonItem = nil;
    }
}
return UISplitViewControllerDisplayModeAutomatic;
}

发生了什么事?

  • 首先,我们检查displayModePrimaryHidden 还是AllVisible,并在此基础上自定义导航按钮。
  • 然后我们在下面检查设备方向,以便可以取消默认的DisplayMode 按钮。

那么我们按钮的选择器方法可以是这样的:

-(void)showMenu{

if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
    if (self.splitViewController.preferredDisplayMode == UISplitViewControllerDisplayModePrimaryHidden) {
        self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
    } else {
        self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
    }
}
else{
    self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
}

[UIView animateWithDuration:0.5 animations:^ {
    [self.splitViewController.view layoutIfNeeded];
}];
}

添加了UIView animation 使其看起来像苹果的动画。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    相关资源
    最近更新 更多