3

This is quite a strange behaviour and I couldn't figure what was wrong out. In iOS7, this code below works as expected but in iOS 8, it has a strange behaviour.

    UIView *mainPopupView = [[UIView alloc] initWithFrame:CGRectMake(10, ([UIScreen mainScreen].bounds.size.height-300)/2-50+20, 300, 380)];
    mainPopupView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:mainPopupView];

In iOS7, these codes add a white mainPopupView to the present view controller, everything is working properly. But in iOS8, after mainPopupView is presented, a black screen (like an UIView with Black blackgroundColor) appears behind mainPopupView. IMO there has some change about the addSubview: method, I tried various searching on Google but no result. Anyone please explains to me why this happens and how to resolve it?

Thanks in advance.

ytbryan
  • 2,644
  • 31
  • 49
Merkurial
  • 242
  • 3
  • 17
  • I'm sorry I didn't describe it properly. The mainPopupView is a subview of self.view, and this "self" is a view controller, assume the class name is PopupViewController. In another controller, I use [self presentViewController:ppvc animated:NO completion:nil] (ppvc is an instance of PopupViewController) and the black background is behind the mainPopupView after ppvc is presented. In ios7, no black background view is shown. So where's the problem :( – Merkurial Oct 07 '14 at 08:28

1 Answers1

1

Try setting the modalPresentationStyle of your PopupViewController instance to UIModalPresentationCustom before presenting it modally.

In case you want to know why this happens, when you present a UIViewController, after the transition animation finishes, the previous view controller is removed from the window hierarchy, since it is not being displayed. When you set the modal presentation style to custom, you are telling the system not to remove the view controller that is presenting. I don't know why it was working pre iOS8.

The dude
  • 7,896
  • 1
  • 25
  • 50
  • 1
    Hey it works, I set it to my instance and it works :x you saved my day dude :x – Merkurial Oct 07 '14 at 09:26
  • So can you please explain to me why I setup the modalPresentationStyle of the viewcontroller in the viewDidLoad: method (not setup the instance) and it doesn't work as expected :( what is the different between these two or was I missing something :( – Merkurial Oct 07 '14 at 15:19
  • @Merkurial I'm not sure but probably viewDidLoad is called after the modal presentation is triggered. Maybe if you place the code in the init method of your VC it would work. Anyway, the presenter view controller is the one who should decide how to present a second view controller, not the second view controller itself. – The dude Oct 08 '14 at 07:23
  • Yeah, you clarified the difference between presenting a vc and a vc itself, thus answered my question :x thank you very much (y) – Merkurial Oct 08 '14 at 14:32