12

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.

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

Sample code or an example is what I'm looking for as well.

Thanks

rs14smith
  • 121
  • 1
  • 1
  • 3

7 Answers7

24

You can create such view by setting appropriate property type of 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];
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Nekto
  • 17,837
  • 1
  • 55
  • 65
  • @ChaseRoberts it will work only on iPad. On iPhone just set `modalPresentationStyle` and `modalTransitionStyle` properties and do not change other values. – Nekto Dec 07 '12 at 07:47
  • @Peter There is no such feature in iOS SDK. – Nekto Jan 16 '13 at 17:15
  • @Nekto Very old thread Thanks!!! but helped me a lot i forgot about superview n it saved me:) – Dalee Davis Feb 05 '13 at 07:17
  • This didn't work for me on iOS 8+. There's now a less hacky way to do this without setting view.superview.frame. See my answer below. – Kento Dec 10 '14 at 17:11
6

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];
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
user523234
  • 14,323
  • 10
  • 62
  • 102
  • 3
    i had the same problem, when i tried this code it threw a exeption - "[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad." – stanley Jun 25 '12 at 07:16
  • This code only works with iPad not with iPhone as in iPhone there is no class called `UIPopoverController` – The iOSDev Dec 18 '15 at 12:52
3

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:

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.

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.

This only works on the iPad.

Kento
  • 452
  • 5
  • 15
1

There is a very good library to display a view controller as Popup on iPhone see here https://github.com/martinjuhasz/MJPopupViewController

Hắc Huyền Minh
  • 1,025
  • 10
  • 13
  • Great stuff. Very practical usage as well. I was looking for something exactly like this. thanks for the link – guinetik Oct 15 '13 at 00:41
1

If you're using Storyboard, you can follow this step:

  1. Add a view controller (V2), setup the UI the way you want it

*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)
  1. Present V2 Modally

  2. Click the segue. In the Attributes inspector, Set Presentation as Over Full Screen. Remove animation if you like

Storyboard

  1. Select V2. In the Attributes inspector, Set Presentation as Over Full Screen. Check Defines Context and Provides Context

Storyboard

  1. Select the MainView of your V2 (Pls. Check image). Set backgroundColor to Clear Color

Storyboard

dhin
  • 460
  • 5
  • 12
0

Create UIView for v2 and add in 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];
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
0

file .m ---> this is the implementation file

-(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

It works for me, hopefully for you...

TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • This has been deprecated and I would not recommend using an Alert as a substitute for modal. –  Feb 24 '16 at 01:04