iPhone Objective C UIView from Nib (xib) in UIScrollView in UITabBarController

So, thats a huge assed title.  Hopefully it got you here from Google tho, as intended.

If you are here, you have probably scoured the internet for an answer to the above title.

I have a UIView that I want in a UIScrollView, that is in a UITabbedView Controller AND I want to load the UIView from a nib (because it makes more sense as it has a much larger height than the UIScrollView it is in).

Ok, so.  Make your UIView in a separate nib file.  Just have it as a plain UIView, with all the goodies within that UIView (the content! labels, buttons! etc!)

Next, you should already have your UITabBarViewController setup.  In the particular Tab you wish to have this UIScrollView in, put nothing.. Should just show the UIViewController and a UITabBarItem inside that.  Set the UIViewController(which has nothing in it)’s type (so, the class that will be loaded with it) to the Controller you already have setup (in attempts to get this to work).  Mine is called AddViewController (as in this view I can ADD something).

Next, within this you want this code..

– (UIView*) createAddCompanyView {
NSArray* nibContents = [[NSBundle mainBundle]
loadNibNamed:@”AddCompanyScrollView” owner:self options:nil];
NSEnumerator *nibEnumerator = [nibContents objectEnumerator];
return (UIView*)[nibEnumerator nextObject];
}

I am adding a company in this view, but you can change the name of the function.

Next, in viewDidLoad (in same ViewController)

– (void)viewDidLoad {
[super viewDidLoad];
contentView = [self createAddCompanyView];
scrollView = [[UIScrollView alloc] initWithFrame:[self.view frame]];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

scrollView.contentSize = CGSizeMake(contentView.frame.size.width, contentView.frame.size.height);

[scrollView addSubview: contentView];
[self.view addSubview:scrollView];

Now you should be able to Build AND GO!  You should see your pretty UIView from the nib loaded and displayed.  If not, make sure it is named correctly and you have swapped out what I called mine one!

NSArray* nibContents = [[NSBundle mainBundle]
loadNibNamed:@”AddCompanyScrollView” owner:self options:nil];

At this point, it scrolls.  Yay!  Thats what I spent all of today getting done, tomorrow, I try to actually USE the content (labels, buttons..) to do something useful.

[Edit] Ok, so I had trouble getting it to work completely.  I had a UIButton at the bottom of the scroll and it wasn’t clicking on it.  I then found I could click on the VERY top of the button.  I decided to move the button up and put a label underneath it (thinking there is a boundry to click events, which the button was below).  It fixed it.

Hooked up my button delegates to point to file owner and viola, everything hunky doray.  These problems I go through remind me very much of Flash development.  Lots of little niggly things you have to hack around to make work.

Posted on February 23, 2009 at 5:06 pm by Jordan Carter · Permalink
In: Iphone · Tagged with: , , , ,

2 Responses

  1. Written by bunnyhero
    on October 11, 2009 at 5:05 pm
    Permalink

    the code snippets aren’t showing up in your blog post. all i see is:

    GeSHi Error: GeSHi could not find the language objc (using path /var/www/blog//wp-content/plugins/codesnippet/lib/geshi/) (code 2)

  2. Written by Jordan
    on October 12, 2009 at 5:04 pm
    Permalink

    Cheers for that, stupid plugin broke..

Leave a Reply