拆分视图控制器提供如下显示模式按钮项:
隐藏主视图并展开详细视图。
我的问题是,有没有办法在代码中模拟此按钮的操作?
答案 0 :(得分: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;
}
发生什么事?
displayMode
是PrimaryHidden
还是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
使其看起来像苹果的动画。