How do I replace a UIView defined in my storyboard before the view is shown?

Discussion in 'iOS Programming' started by moonman239, Aug 14, 2015.

  1. moonman239 macrumors 68000

    Mar 27, 2009
    I have a menu view in my app. Because the layout of the menu is non-uniform and I like being able to use Interface Builder for these items, I have decided to create a UIView subclass called "MenuView" and plaster blank UIViews where the items will go.

    Now, I'd like to know if there's a way to swap each UIView as they're loaded from the UIView.
  2. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    Add outlets for the dummy views into MenuView and connect them up in IB. At run time you'll have access to do what you want, like remove them and add new ones to replace them.

    Alternatively you could just loop through the MenuView subviews and remove them and then add a new varying number of views.
  3. moonman239 thread starter macrumors 68000

    Mar 27, 2009
    Thanks for the suggestion.

    Actually, come to think of it, perhaps I could try overriding addSubview and doing something like this:
    -(void)addSubview:(UIView *)view
        if (viewIsCell)
          MenuViewCell *cell = [[MenuViewCell alloc] initWithFrame:[view frame]];
          [super addSubview:cell];
           [super addSubview:view];
    However, I have also been thinking about simply going with a UICollectionView and implementing a custom layout. Either way, I'll have to figure out how to make sure the app knows where the data goes.
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    You can add your MenuView objects using IB. They just won't look like their real appearance. Add UIViews and then set their custom class to your custom class.

Share This Page