简体   繁体   English

在ScrollView中显示来自xib的视图

[英]Show view from xib in ScrollView

firstly I want to say that I am newbie at iOS development. 首先,我想说我是iOS开发的新手。 I have a problem with showing a view from xib file in ScrollView. 我在ScrollView中显示来自xib文件的视图时遇到问题。

This MyView is only green background with one label. 此MyView是只有一个标签的绿色背景。 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: 这是来自主控制器的代码,其中是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. 我唯一想做的就是在ScrollView中看到MyView(带有一个标签的绿色背景)。 If I run this app, scrollview is empty, nothing is shown inside. 如果我运行此应用程序,scrollview为空,则内部未显示任何内容。

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. 我有一个来自Apple的PageControll应用程序,但对我来说,这是我iOS开头的相当复杂的代码。 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? 它是您的ViewController的财产吗? I think the problem here is that you don't add scroll view to your viewcontroller's view with 我认为这里的问题是您没有使用以下方法将滚动视图添加到viewcontroller的视图中

[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. 最好的祝福。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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