繁体   English   中英

Xcode:如何创建出现在另一个视图控制器中的PopUp视图控制器

[英]Xcode: How To Create A PopUp View Controller That Appears In Another View Controller

基本上我想要弄清楚的是,我说有一个名为V1的视图控制器,里面有一个常规视图和一个按钮。 现在,当您点击该按钮时,我希望该按钮创建一个动作,在同一视图控制器V1中弹出另一个名为V2的视图控制器。

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

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

谢谢

您可以通过设置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];

尝试这个:

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];

如果你想在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更喜欢这个,因为你不需要弄乱箭头方向,用户无法通过点击弹出窗口来关闭它。

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

这仅适用于iPad。

有一个非常好的库来显示视图控制器,因为iPhone上的Popup请看这里https://github.com/martinjuhasz/MJPopupViewController

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

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

*基于您附加的图像

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

  2. 单击segue。 在“属性”检查器中,将“演示文稿”设置为“全屏幕” 如果你愿意,删除动画

故事板

  1. 选择V2。 在“属性”检查器中,将“演示文稿”设置为“全屏幕” 检查定义上下文并提供上下文

故事板

  1. 选择V2的MainView(请检查图像)。 将backgroundColor设置为Clear Color

故事板

v2创建UIView并在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];
}

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 --->头文件中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM