1

I had made a previous project that used UIScrollView with Paging, but with this new project, I have been banging my head over this and can't get it working. I think the problem may possibly lay with AutoLayout. At first I couldn't get the paging motion to even work until I realized that all the initialization has to be done in viewDidAppear instead of ViewDidLoad.

Now that I have the action of paging done, the subView don't load! It will just push the current view I setup in Storyboard out of the way and just shows the blank background.

Note that the code here is almost identical to the my previous project, yet I can't get the 2nd view (page 2) to show up.

Here is some of the code:

- (void)viewDidAppear:(BOOL)animated{
self.scrollView.delegate = self;
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++)
{
    [controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;

// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, (scrollView.frame.size.height - 50));
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;

pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;

// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
//
[self loadScrollViewWithPage:1];
}




- (void)loadScrollViewWithPage:(int)page
{
if (page <= 0)
    return;
if (page >= kNumberOfPages)
    return;

PageOneViewController *controller = [viewControllers objectAtIndex:1];


if ((NSNull *)controller == [NSNull null])
{
    if(page == 1)
    {

        controller = [[PageOneViewController alloc] initWithRequestNumber: @"100" forID:self.idNumberFromLogin];
        [viewControllers replaceObjectAtIndex:page withObject:controller];
        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * 1;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [scrollView addSubview:controller.view];
    }
}

}

In the PageOneViewController

 - (id)initWithRequestNumber:(NSString *)requestID forID: (NSString *) employeeID
{

   if (self = [super initWithNibName:@"OpenRequestCommView" bundle:nil])
   {

   }

   return self;

}
4

3 回答 3

2

您可能在情节提要上启用了自动布局。

您可以通过在情节提要中选择文件检查器并取消选中“使用自动布局”来禁用自动布局。

于 2012-11-16T01:11:27.490 回答
0

您需要将此代码放入您的 .m

#define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

为此更新您的代码

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (IS_WIDESCREEN) {

        self.meuScroll.frame = CGRectMake(0, 0, 320, 468);

    } else {

        self.meuScroll.frame = CGRectMake(0, 0, 320, 380);

    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (IS_WIDESCREEN) {

        self.meuScroll.contentSize = CGSizeMake(320, 468);

    } else {

        self.meuScroll.contentSize = CGSizeMake(320, 380);

    }
}
于 2013-09-12T13:29:30.870 回答
0

上述问题中的代码是基于 Apple 提供的使用 UIScrollView 进行分页的示例代码构建的。我在我的应用程序中使用了相同的代码,并花了几个小时阅读教程、堆栈溢出问题和示例代码,但没有什么可以解决其滚动视图的自动布局问题。事实证明,您只需要在返回 scrollView 作为控制器视图的方法中添加一行代码。(这是示例代码中的 PhoneContentController 类)

- (UIView *)view
{
    [self.scrollView setTranslatesAutoresizingMaskIntoConstraints:YES];
    return self.scrollView;
}
于 2014-09-18T07:09:56.790 回答