Xcode:如何创建一个弹出视图控制器,它出现在另一个视图控制器中。

12

我想要实现的基本上是这样的:假设我有一个名为V1的视图控制器,其中包含一个普通的视图和一个按钮。当你点击该按钮时,我希望该按钮创建一个动作,弹出另一个视图控制器V2,它在同一个视图控制器V1内。

V2 的大小将被减小,以便它不会填满整个屏幕,但你仍然可以看到第一层即V1 在V2后面。所以基本上,你从未真正离开 V1。我希望这能说明我所要做的事情。我知道 MTV 应用程序有这个功能。我正在谈论的图像在这里:https://docs.google.com/leaf?id=0BzlCAVXRsIPcNWUxODM2MDAtNDE3OS00ZTc4LTk5N2MtZDA3NjFlM2IzNmZk&hl=en_US

我也在寻找示例代码或示例。

谢谢

7个回答

24

您可以通过设置modalPresentationStyle属性的适当类型来创建这样的视图。请参阅我下面的示例:

UIViewController *V2 = [[UIViewController alloc] init];
V2.modalPresentationStyle = UIModalPresentationFormSheet;
V2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;     
[V1 presentViewController:V2 animated:YES completion:nil];
V2.view.superview.frame = CGRectMake(0, 0, 540, 620); //it's important to do this after presentModalViewController
V2.view.superview.center = V1.view.center;
[V1 release];

@ChaseRoberts 它只能在iPad上运行。在iPhone上,只需设置 modalPresentationStylemodalTransitionStyle 属性,不要更改其他值即可。 - Nekto
@Peter 在iOS SDK中没有这样的功能。 - Nekto
@Nekto 非常老的帖子,谢谢!但是它对我很有帮助,我忘记了superview,但它救了我 :) - Dalee Davis
这在iOS 8+上对我不起作用。现在有一种更少hacky的方法来做到这一点,而不需要设置view.superview.frame。请参见下面的答案。 - Kento

6

试试这个:

V2 *d = [[V2 alloc]initWithNibName:@"V2" bundle:nil];//assuming V2 is name of your nib as well
d.delegate = self; //Optional:you only need this if you want to delegate

 //create popover and put V2 in the popover view
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:d]; 
popoverController.delegate = self;  //optional
CGSize size = CGSizeMake(325, 75); // size of view in popover…V2
popoverController.popoverContentSize = size;
[d release];
[popoverController presentPopoverFromRect:yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

3
我遇到了相同的问题,当我尝试运行这段代码时,它抛出了一个异常 - "[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad." - stanley
这段代码只适用于iPad,而不适用于iPhone,因为在iPhone上没有名为“UIPopoverController”的类。 - The iOSDev

3

如果你想在iOS 8中以类似于OP截图的风格呈现模态弹出窗口,以下是我的做法:

UIViewController *V2 = [[UIViewController alloc] init];  // V2 is the popup
V2.modalPresentationStyle = UIModalPresentationFormSheet;
V2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
V2.preferredContentSize = CGSizeMake(325, 75); // size of popup view
[V1 presentModalViewController:V2 animated:YES]; // V1 is the current topmost view controller

我认为这种方式比使用UIPopover更好,因为您不需要担心箭头方向,并且用户无法通过在弹出窗口外部轻触关闭它。

这些属性也可以通过设计师在故事板/ Nib中设置。要设置preferredContentSize,请选中“使用首选显式大小”并设置值。

这仅适用于iPad。


1

很棒的东西。非常实用。我正需要这样的东西。谢谢提供链接。 - guinetik

1
如果您正在使用Storyboard,可以按照以下步骤操作:
  1. 添加一个视图控制器(V2),按您想要的方式设置UI

*基于您提供的图片

  • 添加一个UIView - 将背景设置为黑色,透明度为0.5
  • 添加一个UIImageView - 用作您的弹出窗口(请注意,图像和视图不能具有相同的级别/层次结构。不要将imageView设置为view的子级,否则uiview的透明度会影响uiImageView)
  1. 以模态方式呈现V2

  2. 单击segue。在属性检查器中,将Presentation设置为Over Full Screen。如果愿意,可以删除动画

Storyboard

  1. 选择 V2。在属性检查器中,将 Presentation 设置为Over Full Screen。勾选Defines Context and Provides Context

Storyboard

请选择你的V2的MainView(请参考图像)。将backgroundColor设置为Clear Color

Storyboard


0

file .m ---> 这是实现文件

-(IBAction)anyAlert:(id)sender{

   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"A Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK!", @"Other Title", nil];
    [alert show];
    [alert release];
}

记得声明

-(IBAction)anyAlert:(id)sender; 

文件 .h 中 ---> 头文件

对我有效,希望对你也有效...


这已经被废弃了,我不建议使用Alert作为模态框的替代品。 - user3720516

0
创建一个 UIView 用于 v2 并将其添加到 v1 中。
- (void)viewDidLoad
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self 
                   action:@selector(aMethod:)
         forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
        button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.view addSubview:button];
}

- (void)aMethod:(id)sender 
{
    CGRect * imageFrame = CGRectMake(10, 90, 300, 300);
    V2 *v2 = [[V2 alloc] initWithFrame:imageFrame];
    [self.view addSubview:v2];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接