[英]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,则可以按照以下步骤操作:
- Add a view controller (V2), setup the UI the way you want it
添加视图控制器(V2),按照您希望的方式设置UI
*based on the image you attached *基于您附加的图像
Present V2 Modally
礼物V2模态
Click the segue.
单击segue。 In the Attributes inspector, Set Presentation as Over Full Screen .
在“属性”检查器中,将“演示文稿”设置为“全屏幕” 。 Remove animation if you like
如果你愿意,删除动画
- Select V2.
选择V2。 In the Attributes inspector, Set Presentation as Over Full Screen .
在“属性”检查器中,将“演示文稿”设置为“全屏幕” 。 Check Defines Context and Provides Context
检查定义上下文并提供上下文
- 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.