Note that there are some explanatory texts on larger screens.

plurals
  1. POUIViews side by side in a horizontal scroll view
    primarykey
    data
    text
    <p>I'm currently trying to learn the <code>UIScrollView</code>. As an exercise, I simply want <code>UIView</code>s with labels that show texts, and I want those views to be within that <code>UIScrollView</code>. To do this, I have two <code>mutableArray</code>s; one will contain the texts, and the other will contain the <code>UIView</code>s. Below are the methods that initialize these arrays.</p> <pre><code>- (NSMutableArray *)postViewsArray { if (_postViewsArray == nil) _postViewsArray = [[NSMutableArray alloc] init]; return _postViewsArray; } - (NSMutableArray *)postsArray { if (_postsArray == nil) _postsArray = [[NSMutableArray alloc] init]; return _postsArray; } </code></pre> <p>Next, I fill up those two arrays with values. For the <code>postsArray</code> I only placed static texts. But for the <code>postViewsArray</code> I used <code>NSNull</code> as a placeholder. This is for optimization purposes, especially memory management, when I already need to load views with images in the future. So below were the methods to set those arrays up.</p> <pre><code>- (void) setupPostsArray { [self.postsArray addObject:@"Hello"]; [self.postsArray addObject:@"You are doing good"]; [self.postsArray addObject:@"By this exercise"]; [self.postsArray addObject:@"In scroll views, paging enabled :)"]; } - (void) setupPostViewsArray { for (int i = 0; i &lt; [self.postsArray count]; i++) { [self.postViewsArray addObject:[NSNull null]]; } } </code></pre> <p>There, simple. Now let's get to the more challenging part. So here in <code>loadMyPages</code> method, first off I initialize my <code>scrollView</code>, set it's <code>contentSize</code>, and add it as a subview of my top - level view, which is <code>self.view</code>. Here, I also do iteration in creating the "pages" that I can swipe through, it's content, background color. There it is. </p> <p>However, when I run the app, I can only see one <code>view</code> that can be swiped. And it seemed that 3 more <code>views</code> can fit inside this scroll view. I did <code>NSLog</code>s to check the <code>page</code>s being created and it's <code>subviews</code> and it seemed to work fine as I see the logs in the debug area. Note that the <code>view</code> that I can see has the last value of my <code>postArray</code>. A friend suspects that the other <code>view</code>s were overlapped instead of being viewed side by side. </p> <p>Shed some light for me here kind ones :) Below is my <code>loadMyPages</code> method and perhaps take a look on this if you guys are up for the challenge. </p> <pre><code>- (void) loadMyPages { NSLog(@"loads view"); CGRect pagingScrollViewFrame = [[UIScreen mainScreen] bounds]; self.pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame]; self.pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width * [self.postsArray count], pagingScrollViewFrame.size.height); [self.view addSubview:self.pagingScrollView]; // iteration to enumerate creating of UIViews for (int i = 0; i &lt; [self.postsArray count]; i++) { NSLog(@"Creating views?"); UIView *pageView = [self.postViewsArray objectAtIndex:i]; if ((NSNull *) pageView == [NSNull null]) { // create a new UIView UIView *page = [[UIView alloc] initWithFrame:self.view.bounds]; page.backgroundColor = [UIColor lightGrayColor]; // set text label self.postLabel.text = [self.postsArray objectAtIndex:i]; // add the label to the created UIView [page addSubview:self.postLabel]; // then lastly, add each element of the postViewsArray as a subview of the pagingScrollView [self.postViewsArray replaceObjectAtIndex:i withObject:page]; [self.pagingScrollView addSubview:[self.postViewsArray objectAtIndex:i]]; NSLog(@"this view %@ has this post value: %@", page, [page subviews]); NSLog(@"subviews of scrollview: %@", [self.pagingScrollView subviews]); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload