2

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.

Weston
  • 1,481
  • 1
  • 11
  • 31

3 Answers3

2

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!

Jason C. Howlin
  • 3,858
  • 3
  • 21
  • 29
1

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.

Community
  • 1
  • 1
David
  • 9,635
  • 5
  • 62
  • 68
0

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

Jakob Weisblat
  • 7,450
  • 9
  • 37
  • 65
  • I think you need to add a UIView class or maybe a UIViewController. If you add it with the XCode interface and check the ".nib file interface" checkbox, then add a generic UIViewController rather than a generic UIView and change the name to the name of the custom class to that name. does that work? – Jakob Weisblat Jul 28 '12 at 19:27
  • If I change the view controller to the nib, then it works, but I am trying to set the contents of a scrollview this way, Is it just not possible? – Weston Jul 29 '12 at 13:34
  • Sorry - I don't think it is. see http://stackoverflow.com/questions/3432467/add-nib-into-uiscrollview for further information – Jakob Weisblat Jul 29 '12 at 17:20