Weston
Weston

Reputation: 1491

Can I load nib contents in to UIView using only Interface Builder?

Does anyone know a way to, in a storyboard, load a UIView's contents from another nib? I know I can do this easily with code, but I am trying to figure out how to do more in IB.

I have a storyboard with my main UI layout, I have a UIScrollView and I want to design its contents in IB. The only way I could figure out how to do this was to design the UIView in its own .nib, but then my issue is, how do I load the nib without coding it to do so? Is this even possible? It doesn't seem too far fetched to me.

Upvotes: 2

Views: 622

Answers (3)

user1178952
user1178952

Reputation:

I'm assuming you simply want to lay out your UIScrollView in IB, that a .nib is mentioned because that was an approach you were exploring, but if you could do this strictly in your storyboard that would be acceptable, if not preferable:

  1. First, create a new file in Xcode that is a subclass of UIScrollView.

  2. In your storyboard, drag a UIScrollView out to the scene (viewcontroller) where you want to display this scroll view.

  3. In the Identity inspector, set the Custom Class of the UIScrollView to your subclass of UIScrollView.

  4. Create an outlet for this UIScrollView by ctrl+dragging the UIScrollView into the .h file of the ViewController subclass it's displayed in. Name it something like myScrollView

  5. In your ViewController's -viewDidLoad method, set the contentSize property of the UIScrollView to whatever size you want it to be. So it will look something like:

    self.myScrollView.contentSize = CGSizeMake(800,800);

  6. Now, drag out UI objects to your UIScrollView and design.

  7. IMPORTANT: To create outlets to these objects is a little tricky. Let's say you've dragged out a UILabel. You need to manually go into your UIScrollView subclass and add to the .h

    @property (nonatomic, weak) IBOutlet UILabel* myLabel;

and to the .m

@synthesize myLabel = _myLabel;
  1. Now you need to get your outline view on screen along with your storyboard and ctrl+drag FROM YOUR SCROLL VIEW TO YOUR LABEL to create an outlet. This is kind of the reverse of what you're used to.

  2. Now you can reference that outlet from within the viewcontroller or the scrollview subclass . For instance, in the viewcontroller -viewDidLoad you could say:

    self.scrollView.myLabel.text = @"Hello World";

HTH!

Upvotes: 2

David
David

Reputation: 9965

If what you want is to edit inside a scrollview from IB, it's a pain, but doable. Have a look at my answer on this question.

Upvotes: 1

aaazalea
aaazalea

Reputation: 7930

Add a generic UIView in the IB, setting its custom class to the name of your nib file.

Replace GradientControl with the name of your nib file (minus the '.xib'). screenshot

Upvotes: 0

Related Questions