12

基本上我想做的是,假设我有一个视图控制器,称为 V1,它内部有一个常规视图和一个按钮。现在,当您点击该按钮时,我希望该按钮创建一个在同一个视图控制器 V1 中弹出另一个视图控制器(称为 V2)的操作。

V2 将缩小一些,使其不会填满整个屏幕,但您仍然可以看到 V2 后面的第一层 V1。所以基本上,你永远不会真正离开 V1。我希望这对我正在尝试做的事情有意义。我知道 MTV 应用程序有这个功能。我正在谈论的图像在这里:https ://docs.google.com/leaf?id=0BzlCAVXRsIPcNWUxODM2MDAtNDE3OS00ZTc4LTk5N2MtZDA3NjFlM2IzNmZk&hl=en_US

示例代码或示例也是我正在寻找的。

谢谢

4

7 回答 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];
于 2011-09-14T05:24:34.373 回答
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];
于 2011-09-14T18:43:16.970 回答
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 更喜欢这个,因为您不需要弄乱箭头方向,并且用户无法通过在弹出窗口之外点击来关闭它。

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

这仅适用于 iPad。

于 2014-12-02T14:56:08.743 回答
1

有一个非常好的库可以在 iPhone 上将视图控制器显示为 Popup,请参见此处https://github.com/martinjuhasz/MJPopupViewController

于 2012-12-12T02:04:39.890 回答
1

如果您使用 Storyboard,则可以按照以下步骤操作:

  1. 添加视图控制器 (V2),按照您想要的方式设置 UI

*根据您附上的图片

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

  2. 单击转场。在属性检查器中,将 Presentation 设置为Over Full Screen。如果你喜欢删除动画

故事板

  1. 选择 V2。在属性检查器中,将 Presentation 设置为Over Full Screen。检查定义上下文并提供上下文

故事板

  1. 选择您的 V2 的 MainView(请检查图像)。将 backgroundColor 设置为清除颜色

故事板

于 2017-09-20T03:14:25.467 回答
0

创建UIViewv2添加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];
}
于 2012-12-04T04:44:58.917 回答
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; 

file .h--->头文件中

它对我有用,希望对你有用......

于 2011-09-29T13:59:49.343 回答