3

I'm trying to organize a complex xib into multiple view.

Suppose the following scenario:

I have the main view (the green one) that contains two subviews (the red and the yellow ones).
Actually I can create three xib and add the subviews programmatically.
I found a solution a bit smarter: I define two IBOutlet in the main view (green) and connect them to the subviews.

@property (nonatomic, weak) IBOutlet RedView * redView;
@property (nonatomic, weak) IBOutlet YellowView * yellowView;

In the main view implementation, programmatically, I add the subviews:

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.redView.frame = self.frame;
    self.redView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.yellowView.frame = self.frame;
    self.yellowView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    [self addSubview:self.redView];
    [self addSubview:self.yellowView];
}

As you can see, I need to set the current frame size, the autoresizingMask and add the subViews to the parent.

The question:

is there a nice way to perform these operations directly from the xib? I want to load the xib ("loadNibNamed") and obtain the main view (green) already filled with the subviews.

I've tried some solutions but neither works...

edit:
I don't want to add the red and yellow view as subviews directly in IB.
The two subviews are as big as the parent and this make complex to work with Interface Builder (hide the yellow view and work on the red, then hide the red and work on the yellow...).
I'd like to have separate views in the same xib but being able to "compose" them in parent-child tree...

Bart
  • 19,692
  • 7
  • 68
  • 77
Yusef Maali
  • 2,201
  • 2
  • 23
  • 29
  • Yeah, you can put the two subviews inside the green one in the xib. Why are you making them as separate view if you want them to be subviews? – rdelmar Dec 13 '13 at 16:44
  • Suppose the red and the yellow views are as big as the green one, because I apply an horizontal swipe effect so only one child is visible at the time. If I put the two subviews into the parent (ok, this works fine) becames more difficult to use IB (hide the red view and work on the yellow, then hide the yellow and work on the red, and so on...). – Yusef Maali Dec 13 '13 at 16:56
  • You don't need to hide the views, you can just reorder them like Clever Error suggests in his answer. In any case, there's no way for the xib to automatically add the subviews for you, you need to do that in code like you showed. – rdelmar Dec 13 '13 at 17:01
  • That could be a nice feature request ;) Thanks a lot for your answer! – Yusef Maali Dec 13 '13 at 17:08

1 Answers1

0

Instead of creating the red and yellow views by dragging a view into the open space. Drag the view onto the green view. It will then be added as a subview.

You can move your existing views only by using the list on the left. Drag the row in the list that corresponds to the red view onto the row in the list that is the green view.

Craig Siemens
  • 12,942
  • 1
  • 34
  • 51
  • The green view is only a container for the two subviews, that are as big as the parent. The real content is displayed on the subview and I apply a swipe effect on them. Only one subview is visible at the time. I thought to separate the subview in the IB to better work on them. Otherwise I have to hide everytime the views I'm not interested in... I thought to separate the subview in the IB to can better work on them. Otherwise I have to hide all the time, the views I'm not interested in... – Yusef Maali Dec 13 '13 at 17:00