How to open a child view (customized) on parent view with parent view disabled (not the child view)?

StackOverflow https://stackoverflow.com/questions/23391910

  •  12-07-2023
  •  | 
  •  

Question

I want to open a screen (small screen as a child) on the click of a button on the parent screen. I also want to disable the interaction with the parent view until subview is open. I tried to open the child as a subview and if I disable the parent it also disables the subview which make sense.

I also tried to open the subview as a separate view using presentViewController but in that case it totally black out the parent screen.

What is the best way to open a child view? And how to disable the parent view?

Solution:

ParentViewController is the main View Controller and ChildViewController is the subview and it has its only .m and .h file. In this way you can handle your click events on its own classes otherwise it throws exception

In File ParentViewController.h

#import “ParentViewController.h
#import “ChildViewController.h"
@class ChildViewController;

@interface ParentViewController : UIViewController
{
 ChildViewController *childViewController;
}

In File ParentViewController.m

 @implementation ParentViewController`
 -(void)viewDidLoad`
 {
  childViewController  = [[ChildViewController alloc]initWithNibName:@“Child” bundle:nil];
 }

  -(IBAction)btnSubView:(id)sender 
  { 
  UIView *clearView = [[UIView alloc]init];
  clearView.frame = self.view.frame;`
  childViewController.view.frame = CGRectMake(200, 350, 404, 227);
  clearView.backgroundColor = [UIColor colorWithRed:0.0 green:0.1 blue:0.4 alpha:0.2];
  [self.view addSubview:clearView];
  [clearView addSubview:groupViewController.view];
  }
Was it helpful?

Solution

You can open a child subview that obscures the parent (i.e. it is the same width and height, has a grey background, and consumes touches), and then open a subview in that subview that handles your user interaction.

You can make your obscuring view transparent if you want to show the actual parent superview, you just have to make sure in the first-layer subview you catch all touches and don't allow them to be passed to the parent superview. See this post for info: how to prevent a touch event passed to a UIView's superview?

OTHER TIPS

1-create a. xib and customize your .xib file

2-use this code to call child subview

CalloutView *calloutView = (CalloutView *)[[[NSBundle mainBundle] loadNibNamed:@"calloutView" owner:self options:nil] objectAtIndex:0];
            CGRect calloutViewFrame = calloutView.frame;
            calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
            calloutView.frame = calloutViewFrame;

            [calloutView.calloutLabel setText:[(locationAnnotation*)[view annotation] title]];
            [calloutView.CallDattabel setText:[(locationAnnotation*)[view annotation] subtitle]];
            [calloutView.ImageSate setImage:[(locationAnnotation*)[view annotation] image ]];
            [view addSubview:calloutView];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top