Shai Givati
Shai Givati

Reputation: 1156

Add reusable custom UIView to multiple UIViewControllers

I created custom UIView with its NIB, in which I want to use as popup in multiple UIViewControllers. How can I add this custom UIView to existing UIViewController, either by SB or programmatically?

Upvotes: 2

Views: 353

Answers (3)

Monika Patel
Monika Patel

Reputation: 2375

Create one UIViewController and put your custom UIView in that view controller. Add this function in your any static file if your already created other wise add this method in appdelgate.m. AppDelegate.h

@property(strong,nonatomic) BottomPlayerViewVC *nonSystemsController;
-(void)addPlayerView:(UIView*)view;

AppDelegate.m

-(void)addPlayerView:(UIView*)view
{
    [self.nonSystemsController.view removeFromSuperview];
    self.nonSystemsController = [[BottomPlayerViewVC alloc] initWithNibName:@"BottomPlayerViewVC" bundle:nil];
    self.nonSystemsController.view.frame = 'set_your_frame';
    [view addSubview:self.nonSystemsController.view];
}

ViewController1.m

#define AppObj (AppDelegate *)[[UIApplication sharedApplication] delegate]
- (void)viewDidLoad 
{
    [super viewDidLoad];
    [AppObj addPlayerView:self.view];
}

Upvotes: 1

Pavankumar
Pavankumar

Reputation: 288

Look at right in show at identity inspector - add uiview class to there. 2.Programatically - Add uiview class header file into view controller class.

    SampleView *view = [[SampleView alloc]initWithFrame:CGRectMake(100, 20, [UIScreen mainScreen].bounds.size.width-200, 80)];
[self.view addSubview:view];

Upvotes: 1

Bista
Bista

Reputation: 7903

  • Create an XIB file for UIView.

  • Add Labels, buttons to it as per requirements.

  • Create an UIView Class file e.g. Class MyView:UIView.

  • Assign this Class to the UIView via IBInspector.

  • Now create an instance of this class, assign values to the elements.

Upvotes: 3

Related Questions