ios Xcode:如何创建出现在另一个视图控制器中的弹出视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7411383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode: How To Create A PopUp View Controller That Appears In Another View Controller
提问by rs14smith
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. 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 的视图控制器,它内部有一个常规视图和一个按钮。现在,当您点击该按钮时,我希望该按钮创建一个操作,在同一个视图控制器 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. So basically, you never really leave V1. I hope this makes sense for what I'm trying to do. I know the MTV app has this functionity. An image of what I'm talking about is here: https://docs.google.com/leaf?id=0BzlCAVXRsIPcNWUxODM2MDAtNDE3OS00ZTc4LTk5N2MtZDA3NjFlM2IzNmZk&hl=en_US
V2 的尺寸会缩小一些,使其不会填满整个屏幕,但您仍然可以看到 V2 后面的 V1 的第一层。所以基本上,你永远不会真正离开 V1。我希望这对我正在尝试做的事情有意义。我知道 MTV 应用程序具有此功能。我正在谈论的图像在这里:https: //docs.google.com/leaf?id=0BzlCAVXRsIPcNWUxODM2MDAtNDE3OS00ZTc4LTk5N2MtZDA3NjFlM2IzNmZk&hl=en_US
Sample code or an example is what I'm looking for as well.
示例代码或示例也是我正在寻找的。
Thanks
谢谢
回答by Nekto
You can create such view by setting appropriate property type of modalPresentationStyle
. See my example below:
您可以通过设置适当的属性类型来创建这样的视图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];
回答by user523234
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];
回答by Kento
If you want to present this as a modal popup in iOS 8with 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. To set preferredContentSize check "Use Preferred Explicit Size" and set the values.
这些属性也可以通过设计器在故事板/笔尖中设置。要设置 preferredContentSize,请选中“使用首选显式大小”并设置值。
This only works on the iPad.
这仅适用于 iPad。
回答by dhin
If you're using Storyboard, you can follow this step:
如果您使用的是 Storyboard,则可以按照以下步骤操作:
- 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
- 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)
- 添加 UIView - 将背景设置为黑色,将不透明度设置为 0.5
- 添加一个 UIImageView - 这将作为你的弹出窗口(请注意图像和视图不能具有相同的级别/层次结构。不要让图像视图成为视图的子视图,否则 uiview 的不透明度会影响 uiImageView)
Present V2 Modally
Click the segue. In the Attributes inspector, Set Presentation as Over Full Screen. Remove animation if you like
以模态方式呈现 V2
单击转场。在属性检查器中,将 Presentation 设置为Over Full Screen。如果您愿意,请删除动画
- Select V2. In the Attributes inspector, Set Presentation as Over Full Screen. Check Defines Context and Provides Context
- 选择 V2。在属性检查器中,将 Presentation 设置为Over Full Screen。检查定义上下文并提供上下文
- Select the MainView of your V2 (Pls. Check image). Set backgroundColor to Clear Color
- 选择 V2 的 MainView(请检查图像)。将 backgroundColor 设置为清除颜色
回答by H?c Huy?n Minh
There is a very good library to display a view controller as Popup on iPhone see here https://github.com/martinjuhasz/MJPopupViewController
有一个非常好的库可以在 iPhone 上将视图控制器显示为 Popup,请参见此处https://github.com/martinjuhasz/MJPopupViewController
回答by Santiago Montoya
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...
它对我有用,希望对你有用......
回答by Santiago Montoya
Create UIView
for v2
and add in v1
.
创建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];
}