9

I have the following code to create a view and put it in scrollview to allow paging
the code works fine however what I couldn't do is loading views from a nib file

in other words I want to use "initWithNibName" instead of "initWithFrame"?

   - (void)createPageWithColor:(UIColor *)color forPage:(int)page
     {
     UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 300,400)];
         newView.backgroundColor = color;
     [scrollView addSubview:newView];
    }

Thanks alot

1
  • If you WERE to do it this way (and, don't, but if you WERE), you'd want to remember the [newView release] after adding it to scrollView. Your scrollView will retain the view when it's added, so it doesn't really belong to you anymore. Commented Jun 22, 2010 at 12:39

3 Answers 3

21

I know this is an old post, but I wanted to create a UIView as a separate file and add a xib so I could use it in several places in my app (almost like using it as a custom table view cell). And I couldn't get it quite right, but this post helped me get to the result I wanted.

So just if anyone wants to do it the same way, this is what I did:

Add this to the initialization code in your UIView's .m file:

- (id)initWithFrame:(CGRect)frame
{
    self = [NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil][0];
    if (self)
    {
        self.frame = frame;
    }
    return self;
}

Then in interface builder/xib editor you have to assign the class you created for the UIView so you can add IBOutlets.

Hope it helps someone out, cuz it took me a bit!

Sign up to request clarification or add additional context in comments.

2 Comments

No need to make a new CGRect, you can simply do self.frame = frame
Fantastic! Great solution!
14

I think the thing you're missing is that you can set the frame of your new UIView after loading the nib. Load/Init time isn't your only shot at that. I'm also breaking the load function into its pieces, in the code below, so you can see more easily what's going on.

Observe:

NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"yournib" 
                                                     owner:self 
                                                   options:nil];
//I'm assuming here that your nib's top level contains only the view 
//you want, so it's the only item in the array.
UIView *myView = [nibContents objectAtIndex:0];
myView.frame = CGRectMake(0,0,300,400); //or whatever coordinates you need
[scrollview addSubview:myView];

Don't forget that for that UIScrollView to actually scroll, you need to set its contentSize property to the size of the goods inside it, which is likely bigger than the .frame property of the scroll view itself.

1 Comment

Thats what I am talking about :) Thank you Dan, your solution works perfectly, yes I have changed the positions of the views on the scrollview with something like "scrollView.frame.size.width * page" for the x coordinator Thanks again, you made my day :)
4

Try something like this (adapted from "The iPhone Developers Cookbook", pg. 174):

UIView *newView = [[[NSBundle mainBundle] loadNibNamed:@"yournib" owner:self options:nil] lastObject];

This assumes a single view object in your .xib, but you could modify it if your .xib is more complicated.

2 Comments

Great John, but I need to specify the position of the newView because I am adding it to a scrollview
Commented about something @John N missed but I missed that he HADN'T missed it. So never mind!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.