1

I'm looking for a way to change the appearance of a child view controller at runtime.

I see there are possibilities for giving a view controller a modal presentation style (here and here) but non of this seems to work if the view controller is already added to the view hierarchy.

I've tried to make all views recursively semi transparent:

- (void)showWithTransparency:(CGFloat)opacity
{
  [self recursiveSetBackgroundColorOpacityInView:self.view opacity:opacity];
}


- (void)recursiveSetBackgroundColorOpacityInView:(UIView*)inView opacity:(CGFloat)opacity;
{
  CGFloat red, green, blue, alpha;

  if(inView.backgroundColor != UIColor.clearColor)
  {
    [inView.backgroundColor getRed:&red green:&green blue:&blue alpha:&alpha];
    inView.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:opacity];
  }

  for(UIView* view in inView.subviews)
  {
    if(view.backgroundColor != UIColor.clearColor)
    {
      [view.backgroundColor getRed:&red green:&green blue:&blue alpha:&alpha];
      view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:opacity];
    }

    [self recursiveSetBackgroundColorOpacityInView:view opacity:opacity];
  }
}

This works not very well because I don't store the previous opacity of a view and restore it afterwards. Hence, some view won't restore correctly.

Do you have any hints how to use modalPresentationStyle on my child view controller so I could switch it on an off at runtime?

Do you have any hints how I could make my child view controller semi transparent at runtime and restore it with other UIKit possibilities?

Bruno Bieri
  • 8,893
  • 10
  • 60
  • 83

0 Answers0