5

I have a two UIViewControllers, and I am displaying the secondViewController modally over the firstViewController. My secondViewController contains a subview which is UIView with some buttons. Now what I want to do is, make the SecondViewController semi transparent so that my firstViewController is visible and only show the subview modally which is not transparent.

Reagards Ranjit

Ranjit
  • 4,576
  • 11
  • 62
  • 121
  • what have you tried have you even tried to do google??? check this :-http://stackoverflow.com/questions/5795688/iphone-create-a-semi-transparent-rectangle-with-opaque-text – Leena Jul 19 '12 at 13:02
  • Hi @Leena I have made my view transparent my making it as a subview and not as modalView, but now I am not understanding, how can I use this to pass data . so any help might be greatful – Ranjit Jul 24 '12 at 11:33
  • can you tell me what problems you are facing??? – Leena Jul 24 '12 at 12:03
  • Hi @Leena : I have sorted the issues, thanks – Ranjit Jul 24 '12 at 13:38

2 Answers2

4

when you present your view controller, view of previous controller is removed so if you set any alpha for your current controller's view, you will get UIWindow's background.

if you intend to play with the transparency, then instead of doing presentModalViewController, in first viewcontroller, [self.view addSubView:controller2.view]; and make controller2.view.alpha = 0.5;//whatever transparency level u want

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
  • Hi samFisher, if I make controller2 as a subView , then can I pass data from controller 2 to controller1 because, viewdidappear will not be called . – Ranjit Jul 19 '12 at 12:48
  • Hey if I apply your solution , the alpha value is applied to the subView present in controller2 which I dont want. – Ranjit Jul 19 '12 at 12:52
  • well.. for that you can create and implement your own protocol-delegate mechanism. What I ment like there are alternative ways of passing data from controller1 to controller2 but not for the transparency thing that you are looking for.. – Saurabh Passolia Jul 19 '12 at 12:53
  • right.. you can set ur controller2.view.color = clear color and set another view over your controller's primary view and set its alpha value – Saurabh Passolia Jul 19 '12 at 12:54
  • can I call viewdidAppear, like [firstcontroller viewdidAppear]?. – Ranjit Jul 19 '12 at 13:23
  • Hey, now the firstViewController is totally visible, I want to be semivisible. – Ranjit Jul 19 '12 at 13:25
0

If you're targeting iOS 5.0 and above, you can use the container view controller approach:

// create the modal view controller
MyModalController *modal = [[MyModalController alloc] 
  initWithNibName:@"MyModal" bundle:nil];
[modal willMoveToParentViewController:self];

// add it to the controllers and views hierarchies
[self addChildViewController:modal];
modal.view.frame = self.view.bounds;
[self.view addSubview:modal.view];
[modal didMoveToParentViewController:self];

Then whatever alpha you set for the main view's background in IB or in code will be respected.

sobri
  • 1,626
  • 15
  • 28