0

I have a custom UIView that I've created in a xib file. Now in another view controller, I have added a scroll view in the storyboard. In the implementation file for this view controller, I am trying to add this custom UIView, but it is not showing up. I'm sure I am missing something since I am still getting used to working with xib files. I have tried adding normal UIViews and those show up properly in the scroll view. This is the code:

[[NSBundle mainBundle] loadNibNamed:@"CustomUIView" owner:nil options:nil];
for (int i = 0; i < 10; i++) {
    CustomUIView *setView = [[CustomUIView alloc] initWithFrame:CGRectMake(10, 10 + (10*i) + (i*100), self.view.frame.size.width - 20, 100)];

    [self.scrollView addSubview:setView];
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 10 + (10*10) + (10*100));

The positioning for the frames worked for programmatically created UIViews in this loop so I don't think that is the issue. Any ideas? Thanks.

user3129120
  • 113
  • 11
  • 1
    Any reason why you are adding multiple instances of that subview inside the scrollview via loop? Why not make it a UITableViewCell and use UITableView instead? – NSNoob Oct 28 '15 at 07:00
  • I could try that, but I was making a more different type of setup and didn't want to be restricted by using a table view. Does the code I'm using look okay though? – user3129120 Oct 28 '15 at 07:02
  • I don't think so, no. There is a lot of room for things to go wrong in its current state. But especially that bit about how you are loading the nib seems wrong. – NSNoob Oct 28 '15 at 07:33
  • Alright I ended up switching to a table view and that works. Thanks for the help. – user3129120 Oct 28 '15 at 07:36
  • 1
    Interesting. I think I found out an answer for your real issue as well. If you want, you can try it. See here, http://stackoverflow.com/questions/13534502/ios-loadnibnamed-confusion-what-is-best-practice – NSNoob Oct 28 '15 at 07:38

2 Answers2

0

Use this code

for (int i = 0; i < 10; i++) {
    CustomUIView *setView = [[[NSBundle mainBundle] loadNibNamed:@"CustomUIView" owner:nil options:nil] objectAtIndex:0];
    setView.frame = CGRectMake(10, 10 + (10*i) + (i*100), self.view.frame.size.width - 20, 100);

    [self.scrollView addSubview:setView];
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 10 + (10*10) + (10*100));
KavyaKavita
  • 1,581
  • 9
  • 16
0

In your specific case, the better approach would be to make the UIView a UITableViewCell and use it to populate UITableView instead of running a loop and adding subviews in a UIScrollView. I am glad that it solved your problem and made your App better at the same time.

But approach apart, if you want to know why is your code not working, the problem is how you are loading the Nib file. This already answered question will clear all your concepts about loading nibs and also show you the correct way of loading it in the most stable way. Happy Coding!

Community
  • 1
  • 1
NSNoob
  • 5,548
  • 6
  • 41
  • 54