有没有办法以编程方式隐藏 splitviewcontroller 中的主视图。在我的应用程序中,第一个屏幕将是 splitviewcontroller,下一个屏幕我不需要任何拆分视图。我怎么能做到这一点
9 回答
in SDK 5.0 they added new method for UISplitViewControllerDelegate that would do this easily for you. Just implement it like next and you would not see the master view:
- (BOOL)splitViewController:(UISplitViewController*)svc
shouldHideViewController:(UIViewController *)vc
inOrientation:(UIInterfaceOrientation)orientation
{
return YES;
}
The only place when you can see it is rotation - the part of master view is visible during animation. I've fixed that in simple way, just loaded empty and black view in master.
PS: not sure whether this is actual for i0707. But hope this could be useful for other people that now have the same issues.
与杰克的回答相同,但只有一条线。过去进入 - (void)setDetailItem:(id)newDetailItem { ... } 以解雇主人。
[[UIApplication sharedApplication] sendAction: self.navigationItem.leftBarButtonItem.action
to: self.navigationItem.leftBarButtonItem.target
from: nil
forEvent: nil];
SplitViewController 提供的 BarButtonItem 是以编程方式隐藏 Master View Controller 的关键。
这段代码很危险!但优雅:)
导入objective c消息库
#import <objc/message.h>
接下来,获取 SplitViewController 提供的 UIBarButtonItem 的句柄
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem
forPopoverController:(UIPopoverController *)popoverController
{
barButtonItem.title = @"Master";
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
//Obtain handle to BarButtonItem
[self setMasterButtonItem:barButtonItem];
}
然后当触发事件时应该触发主视图控制器的自动关闭,即
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
你可以这样做
objc_msgSend(self.masterButtonItem.target, self.masterButtonItem.action);
Matt Gemmell 创建了一个出色的自定义 splitViewController,称为“MGSplitViewController”。它非常容易实现,注释很多,并且包含许多普通 splitViewController 所没有的优秀功能(在横向视图上隐藏主视图,在横向视图中更改拆分的位置,允许用户在运行时流畅地更改拆分的大小, ETC)。
信息和演示: http: //mattgemmell.com/2010/08/03/mgsplitviewcontroller-updated/
但是,当我尝试时,上面的代码对我不起作用
CGRect selfFrame = self.splitViewController.view.frame;
它做了。所以..这对我有用..(这段代码应该在你的detailviewcontroller中)
-(void)hideMaster {
UIViewController *masterController = GetAppDelegate().masterController;
CGRect selfFrame = self.splitViewController.view.frame;
CGFloat aWidth = masterController.view.frame.size.width;
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.30f];
if(orientation == UIDeviceOrientationLandscapeLeft)
{
selfFrame.size.height += aWidth;
selfFrame.origin.y -= aWidth;
}
else if(orientation == UIDeviceOrientationLandscapeRight)
{
selfFrame.size.height += aWidth;
}
[self.splitViewController.view setFrame:selfFrame];
[UIView commitAnimations];
}
为了允许旋转,这是需要的:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration
{
[super willAnimateRotationToInterfaceOrientation:interfaceOrientation duration:duration]; // not sure if needed
//By the time this method is called the bounds of the view have been changed
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
if (masterIsHidden) {
[self showMaster];
}
} else {
if (self.editing) {
[self hideMaster];
}
}
[UIView setAnimationDuration:duration];
[UIView commitAnimations];
}
尝试这个:
splitVC.modalPresentationStyle = UIModalPresentationFullScreen;
[splitVC presentModalViewController:[[splitVC viewControllers] objectAtIndex:1] animated:NO];
对我来说适用于 4.2!
这是另一个很棒的技巧。视频链接在这里。
虽然它不会有很好的转换(抱歉),但您可以通过将根视图设置为详细视图控制器的视图来实现此目的,然后将视图与 UISplitView 交换并将详细视图移动到 UISplitView。(实际上,您可能能够为视图交换设置动画(推/翻转/等),但在视图更改动画期间更改任何内容是个坏主意,并且将详细视图移动到 UISplitView 内部可能符合条件。)
不知道这是不是你要找的。例如,要在单击按钮时以横向模式隐藏主视图,您可以执行以下操作(在选择器方法中)
UIViewController *master = [splitViewController.viewControllers objectAtIndex:0];
UIViewController *detail = [splitViewController.viewControllers objectAtIndex:1];
[master.view setFrame:CGRectMake(0, 0, 0, 0)];
detail.view.frame = splitViewController.view.bounds; // or use master and detail length
这有效:
将“隐藏”方法附加到您的按钮,例如:
UIBarButtonItem *hideButton = [[UIBarButtonItem alloc] initWithTitle:@"hide"
style: UIBarButtonItemStylePlain
target: self
action: @selector(hide:)
];
[self.mainView.navigationItem setLeftBarButtonItem:hideButton];
在这段代码中,“self.mainView”是 splitview 的第二个视图中导航控制器中的视图控制器 - 仅作为参考。
hide 方法看起来像这样。
-(void)hide:(id)sender
{
UIViewController *masterController = [self.viewControllers objectAtIndex:0];
CGRect selfFrame = self.view.frame;
CGFloat aWidth = masterController.view.frame.size.width;
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.30f];
if(orientation == UIDeviceOrientationLandscapeLeft)
{
selfFrame.size.height += aWidth;
selfFrame.origin.y -= aWidth;
}
else if(orientation == UIDeviceOrientationLandscapeRight)
{
selfFrame.size.height += aWidth;
}
[self.view setFrame:selfFrame];
[UIView commitAnimations];
}
这是起点,显然需要做更多的逻辑来处理旋转,再次显示等等......
我希望这有帮助。
通过 iOS5 和 Xcode 4.3 测试