无法将自定义视图控制器添加到 UIScrollVIew

发布于 2025-01-05 02:07:11 字数 1039 浏览 5 评论 0原文

我正在尝试创建一个使用 UIScrollView 在不确定数量的动态生成的自定义视图控制器之间进行分页的应用程序。不幸的是,当我尝试下面所示的代码时,我得到的只是一个空白屏幕。此外,我在子视图之后放置的 nslog 语句据说被添加到 UIScrollView 中,总是说 UIScrollView 有零个子视图。我对这个问题完全感到困惑,非常感谢任何帮助。

- (void)loadView 
{
   [super loadView];

   scrollView.pagingEnabled = YES;

   viewControllers = [[NSMutableArray alloc] init];

   CGRect screenBounds = [[UIScreen mainScreen] bounds];
   CGFloat screenScale = [[UIScreen mainScreen] scale];

   CGFloat width = screenBounds.size.width * screenScale;
   CGFloat height = screenBounds.size.height * screenScale;

   scrollView.contentSize = CGSizeMake(width * [routes count], height);

    for( int i = 0; i < [arrayWithDataForViewControllers count]; i++)
    {
       ViewController *controller = [[ViewController alloc] init];

       controller.view.frame = CGRectMake(width*i, 20, width, height);

        [scrollView addSubview:controller.view];
         NSLog(@"SUBVIEWS %d", [[scrollView subviews] count]);
         [viewControllers addObject:controller];
    }   
}

I'm trying to make an application that uses a UIScrollView to page between an indeterminate number of dynamically generated custom view controllers. Unfortunately, when I try the code shown below, all I get is a blank screen. Additionally, the nslog statement that I put right after the subview is supposedly added to the UIScrollView always says that the UIScrollView has zero subviews. I'm totally baffled by this problem and would greatly appreciate any help.

- (void)loadView 
{
   [super loadView];

   scrollView.pagingEnabled = YES;

   viewControllers = [[NSMutableArray alloc] init];

   CGRect screenBounds = [[UIScreen mainScreen] bounds];
   CGFloat screenScale = [[UIScreen mainScreen] scale];

   CGFloat width = screenBounds.size.width * screenScale;
   CGFloat height = screenBounds.size.height * screenScale;

   scrollView.contentSize = CGSizeMake(width * [routes count], height);

    for( int i = 0; i < [arrayWithDataForViewControllers count]; i++)
    {
       ViewController *controller = [[ViewController alloc] init];

       controller.view.frame = CGRectMake(width*i, 20, width, height);

        [scrollView addSubview:controller.view];
         NSLog(@"SUBVIEWS %d", [[scrollView subviews] count]);
         [viewControllers addObject:controller];
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女情怀诗 2025-01-12 02:07:11

检查滚动视图之外的 ViewController 视图 - 很可能它的 view 属性为零。

Check your ViewController's view outside of the scrollview - chances are that it's view property is nil.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文