简体   繁体   English

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

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

Basically what I am trying to figure out to do is, say I have one View Controller, called V1, that has a regular view inside it and a button. 基本上我想要弄清楚的是,我说有一个名为V1的视图控制器,里面有一个常规视图和一个按钮。 Now, when you tap that button, I want that button to create an action that pop-ups another View Controller, called V2, within the same view controller, V1. 现在,当您点击该按钮时,我希望该按钮创建一个动作,在同一视图控制器V1中弹出另一个名为V2的视图控制器。

V2 will be reduced in size some so that it does not fill the entire screen, but you can still see the first layer which is V1 behind V2. V2的大小会减小一些,以至于它不会填满整个屏幕,但你仍然可以看到V2后面的第一层是V1。 So basically, you never really leave V1. 所以基本上,你永远不会离开V1。 I hope this makes sense for what I'm trying to do. 我希望这对我正在尝试做的事情有意义。 I know the MTV app has this functionity. 我知道MTV应用程序具有这种功能。 An image of what I'm talking about is here: https://docs.google.com/leaf?id=0BzlCAVXRsIPcNWUxODM2MDAtNDE3OS00ZTc4LTk5N2MtZDA3NjFlM2IzNmZk&hl=en_US 我正在谈论的图片如下: https//docs.google.com/leaf?id = 0BzlCAVXRsIPcNWUxODM2MDAtNDE3OS00ZTc4LTk5N2MtZDA3NjFlM2IzNmZk&hl = en_US

Sample code or an example is what I'm looking for as well. 示例代码或示例也是我正在寻找的。

Thanks 谢谢

You can create such view by setting appropriate property type of modalPresentationStyle . 您可以通过设置modalPresentationStyle相应属性类型来创建此类视图。 See my example below: 请参阅下面的示例:

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

Try this: 尝试这个:

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

If you want to present this as a modal popup in iOS 8 with a similar style to the OP's screenshot here's what I did: 如果你想在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

I like this better than using a UIPopover because you don't need to mess with arrow directions and the user cannot close it by tapping outside of the popup. 我比使用UIPopover更喜欢这个,因为你不需要弄乱箭头方向,用户无法通过点击弹出窗口来关闭它。

These properties can also be set in a storyboard/nib via the designer. 这些属性也可以通过设计器在storyboard / nib中设置。 To set preferredContentSize check "Use Preferred Explicit Size" and set the values. 要设置preferredContentSize,请选中“使用首选显式大小”并设置值。

This only works on the iPad. 这仅适用于iPad。

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

If you're using Storyboard, you can follow this step: 如果您使用的是Storyboard,则可以按照以下步骤操作:

  1. Add a view controller (V2), setup the UI the way you want it 添加视图控制器(V2),按照您希望的方式设置UI

*based on the image you attached *基于您附加的图像

  • add an UIView - set background to black and opacity to 0.5 添加UIView - 将背景设置为黑色,将不透明度设置为0.5
  • add an UIImageView - that will serve as your popup (Pls take note that the image and the view must not have the same level/hierarchy. Dont make the imageview the child of the view otherwise the opacity of the uiview will affect the uiImageView) 添加一个UIImageView - 它将作为你的弹出窗口(请注意,图像和视图不能具有相同的级别/层次结构。不要使imageview成为视图的子视图,否则uiview的不透明度将影响uiImageView)
  1. Present V2 Modally 礼物V2模态

  2. Click the segue. 单击segue。 In the Attributes inspector, Set Presentation as Over Full Screen . 在“属性”检查器中,将“演示文稿”设置为“全屏幕” Remove animation if you like 如果你愿意,删除动画

故事板

  1. Select V2. 选择V2。 In the Attributes inspector, Set Presentation as Over Full Screen . 在“属性”检查器中,将“演示文稿”设置为“全屏幕” Check Defines Context and Provides Context 检查定义上下文并提供上下文

故事板

  1. Select the MainView of your V2 (Pls. Check image). 选择V2的MainView(请检查图像)。 Set backgroundColor to Clear Color 将backgroundColor设置为Clear Color

故事板

Create UIView for v2 and add in v1 . 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 ---> this is the implementation file 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];
}

remember declare 记得申报

-(IBAction)anyAlert:(id)sender; 

in the file .h ---> header file file .h --->头文件中

It works for me, hopefully for you... 它对我有用,希望对你有用......

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

相关问题 在当前视图控制器出现之前到达另一个视图控制器 - Got to another view controller before the current view controller appears xcode UICollection查看打开另一个视图控制器 - xcode UICollection View opening another view controller Swift - 转换到另一个视图控制器(不是弹出窗口) - Swift - Transitions to another View Controller (not popup) 如何在xcode中从一个表视图控制器导航到另一个表视图控制器 - how to navigate from one table view controller to another table view controller in xcode Xcode如何将另一个视图控制器的布尔值设置为no? - Xcode how do I set this bool from another view controller to no? 如何在另一个视图控制器中控制对象? (Xcode:objective-c) - How to control objects in another view controller? (Xcode:objective-c) 将QR Code加载到另一个视图控制器(Xcode) - Load QR Code to another view controller (Xcode) 从xcode中的另一个视图控制器禁用按钮 - disable button from another view controller in xcode 如何在创建新的视图控制器后创建视图控制器文件? (xcode 8.21) - How to create a view controller file after creating a new view controller? (xcode 8.21) 如何从Xcode中的筛选数组创建表视图控制器? - How to create table view controller from filtered arrays in Xcode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM