简体   繁体   中英

Show view from xib in ScrollView

firstly I want to say that I am newbie at iOS development. I have a problem with showing a view from xib file in ScrollView.

This MyView is only green background with one label. Here is a code from its controller:

@implementation MyView

  - (id)initWithFrame:(CGRect)frame
  {
      self = [super initWithFrame:frame];
      if (self) {
      //init code
      }
      return self;
   }

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

   return self;
   }

@end

And here is a code from main controller where is the ScrollView:

 - (void)viewDidLoad
{
   [super viewDidLoad];

   scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 67, self.view.frame.size.width, self.view.frame.size.height)];
   scroll.pagingEnabled = YES;
   scroll.frame = CGRectMake(0, 20, 320, 350);
   scroll.contentSize = CGSizeMake(320, 350);
   scroll.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
   scroll.bounces = YES;
   scroll.bouncesZoom = YES;
   scroll.scrollEnabled = YES;
   scroll.minimumZoomScale = 0.5;
   scroll.maximumZoomScale = 5.0;
   scroll.delegate = self;
   scroll.pagingEnabled = YES;

   NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil];    
   UIView *myView = [nibContents objectAtIndex:0];
   myView.frame = CGRectMake(0,20,300,300);

   [scroll addSubview:myView];

}

Only thing I want to do is that I want to see MyView (green background with one label) inside the ScrollView. If I run this app, scrollview is empty, nothing is shown inside.

Probably it is a stupid question but as I said, I am newbie at this. I have a PageControll app from Apple but for me this is quite complicated code in my iOS beginning. Of course I was googling... Please tell me where is a problem. Thank you

Where do you define scroll? Is it a property of your viewcontroller? I think the problem here is that you don't add scroll view to your viewcontroller's view with

[self.view addSubview:scroll]

Everything seems to right. Just retain the view in your class that you are receiving from the following line and add that as a subview to your scrollview.

self.myView = [nibContents objectAtIndex:0];

Best Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM