2

I'm trying to add a View created through a xib File into a UIScrollview.

The View looks like that: View setup in Interface Builder

What I get is: The View added to a UIScrollview in code

The Code I use to add the View to the UIScrollview (which is setup in Storyboard) is:

Initialisation code:

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"SpuelungProtokollView" owner:self options:nil];
HPSpuelungProtokollView * spuelungProtokollView = [subviewArray objectAtIndex:0];
HPSpuelung * vInfo = pInfo;

// setup Infos
// ... this part is not related to the problem ...

[self setContentView:spuelungProtokollView];

and then I do the following in viewDidLoad::

[[self scrollView] setContentSize:[[self contentView] frame].size];
[[self scrollView] addSubview:[self contentView]];
[[self contentView] layoutIfNeeded];

Has anybody had a similar Problem - know how to properly add a (properly constraint) UIView to UIScrollview?

Thanks in Advance for Answers!

EDIT

A strange Side Effect is: the View loads correctly if I put the view Hirarchy is created in viewDidAppear:

sensslen
  • 780
  • 8
  • 17
  • What does your awakeFromNib look like? – Bejmax Jan 14 '13 at 14:19
  • I don't have any - what is that function good for? Sorry for beeing a nOOb with xib's until now I did everything with storyboards where I never used this Method – sensslen Jan 14 '13 at 14:26
  • i added an answer, read up on the protocol on the apple dev site, they have examples as well. Best of luck! – Bejmax Jan 14 '13 at 14:27

2 Answers2

1

You should implement awakeFromNib: to initialize the view. If you aren't, then it's probably not getting setup properly.

NSNibAwaking Protocol Reference: This informal protocol consists of a single method, awakeFromNib. Classes can implement this method to initialize state information after objects have been loaded from an Interface Builder archive (nib file).

So, i think you'd want something like the following for your impl:

- (void)awakeFromNib {
    [[self scrollView] setContentSize:[[self contentView] frame].size];
    [[self scrollView] addSubview:[self contentView]];
    [[self contentView] layoutIfNeeded];
    return;
}

awakeFromNib is actually special in that it's part of the NSNibAwaking informal protocol (basically means that your object doesn't have to conform to it). What happens is when an object is loaded from a nib file (.xib) it will be sent this message once all the outlets are connected as long as it implements the awakeFromNib method.

Bejmax
  • 945
  • 7
  • 16
  • Since I'm creating the View in `prepareForSegue: sender:` how do I know which function is called first, `viewDidLoad:` - which assures the ScrollView is there - and `awakeFromNib:` - which assures the Nib is loaded correctly? – sensslen Jan 14 '13 at 14:44
  • look at [this answer](http://stackoverflow.com/a/6303539/1354100) for that question. ;) Essentially viewDidLoad is called first. – Bejmax Jan 14 '13 at 14:58
  • Unfortunately according to my testing this is not true - I guess this is because the Methodes are called to indicate the State of different Views - viewDidLoad is called when the Storyboard xib is decompressed and awakeFromNib is called when the container View is ready... – sensslen Jan 14 '13 at 15:08
  • I just tried to initialize the subview in `viewDidLoad` of the Main view controller in order to be sure of the call order. Even that did not change much. – sensslen Jan 14 '13 at 15:17
1

I've been able to resolve the Issue - I had to set the Autoresizing Mask of the custom View in initWithCoder: like that:

- (id) initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    }

    return self;
}

Hope this helps somebody else....

sensslen
  • 780
  • 8
  • 17