iOS Nested UIScrollViews Not Responding - ios

I've been searching around on how to nest UIScrollViews.
It seems like it should be as easy as adding the inner scroll views by using addSubview: of the container scroll view. I have everything showing up visually correct but the functionality of the inner scroll views is non existent despite supplying them with proper frames and content sizes.
My code below shows what I have so far. Only the outer scroll view scrolls. I want it so that the outer scroll view controls left to right scrolling and each inner scroll view controls vertical scrolling of their related page content.
self.containerScroll = [[UIScrollView alloc]init];
self.containerScroll.frame = CGRectMake(0,(self.headerView.frame.size.height + self.pageControl.frame.size.height),screenWidth, (screenHeight - (self.headerView.frame.size.height + self.pageControl.frame.size.height)));
self.containerScroll.backgroundColor = [UIColor clearColor];
self.containerScroll.alpha = 1;
self.containerScroll.pagingEnabled = YES;
self.containerScroll.contentSize = CGSizeMake(self.containerScroll.bounds.size.width*3,1);
self.containerScroll.bounces = NO;
self.containerScroll.delegate = self;
[self.view addSubview:self.containerScroll];
self.page1Scroll = [[UIScrollView alloc]init];
self.page1Scroll.frame = CGRectMake(0,0,self.containerScroll.bounds.size.width,self.containerScroll.bounds.size.height);
self.page1Scroll.backgroundColor = [UIColor redColor];
self.page1Scroll.alpha = 1;
[self.page1Scroll addSubview:self.feedPageVC.view];
self.page1Scroll.contentSize = CGSizeMake(320,500);
self.page1Scroll.delegate = self;
[self.containerScroll addSubview:self.page1Scroll];
self.page2Scroll = [[UIScrollView alloc]init];
self.page2Scroll.frame = CGRectMake(self.containerScroll.bounds.size.width,0,self.containerScroll.bounds.size.width,self.containerScroll.bounds.size.height);
self.page2Scroll.backgroundColor = [UIColor greenColor];
self.page2Scroll.delegate = self;
[self.containerScroll addSubview:self.page2Scroll];
self.page3Scroll = [[UIScrollView alloc]init];
self.page3Scroll.frame = CGRectMake(self.containerScroll.bounds.size.width*2,0,self.containerScroll.bounds.size.width,self.containerScroll.bounds.size.height);
self.page3Scroll.backgroundColor = [UIColor blueColor];
[self.page3Scroll addSubview:self.detailsPageVC.view];
self.page3Scroll.contentSize = CGSizeMake(320,500);
self.page3Scroll.delegate = self;
[self.containerScroll addSubview:self.page3Scroll];

self.containerScroll.contentSize = CGSizeMake(self.containerScroll.bounds.size.width*3,1);
Looks like the height of your content is set to 1. If a child is larger than its parent, it won't function correctly (works the same for buttons).
Also, make sure you know which scrollview you're handling in the delegate methods. All the scrollviews will be handled in the same method and it could cause you problems.

Related

iOS Reproduce a view n times

I have a static UITableViewController who has 3 what I call Widgets in them. These widgets display data about a game (score, players etc).
In one of my widget I want to display all the players and there icon. I don't know how many players need to be set because this depends on the result the server gives me.
So what I want is to add N times a UIView for all the players the servers returns. I had an idea to do this in a UITableView, but I read somewhere that I cannot add a UITableView inside a TableViewCell.
I would like to be able to make a "Dummy" UIView inside IB and copy and add this for the amount of players or isn't this possible?
I would recomend you to do it in code, it's easy you can create subclass on UIView and you can add UIScrollView. You can override initWithFrame to accept array with yours views:
-(id)initWithFrame:(CGRect)frame data:(NSArray*)myViews
{
if (self = [super initWithFrame:frame])
{
_viewsArray = myViews;
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.backgroundColor = [UIColor clearColor];
[self.scrollView setShowsHorizontalScrollIndicator:NO];
[self addSubview:self.scrollView];
for (int i = 0; i < _viewsArray.count; i++)
{
// This view (v) needs to initialised base on your array content
id obj = _viewsArray[i];
UIView *v = [[UIView alloc] init];
[self.scrollView addSubview:v];
}
}
return self;
}
The last bit left to do is set up a frame for views and scrollView and contentSize in layoutSubviews:
-(void)layoutSubviews
{
[self.scrollView setFrame:self.bounds];
// change your views frames if you need
[self.scrollView setContentSize:CGSizeMake(self.frame.size.width, _viewsArray.count * VIEWHEIGHT)];
}

Using Scroll View and Page Controller for tutorial screen

I want to make a tutorial screen and have put a scroll view and a Page Controller. I want to put image so that the user can swipe through the tutorial screen on first boot. However I can put labels like this
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
float defWidth = _scrollView.frame.size.width;
float defHeight = _scrollView.frame.size.height;
_scrollView.pagingEnabled = YES;
_scrollView.contentSize = CGSizeMake(defWidth * _pageControl.numberOfPages, _scrollView.frame.size.height);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.scrollsToTop = NO;
_scrollView.bounces = NO;
_scrollView.delegate = self;
UIColor *bgColor[] = {[UIColor orangeColor],[UIColor brownColor],[UIColor grayColor],[UIColor darkGrayColor],[UIColor blackColor]};
for (int i = 0; i < _pageControl.numberOfPages; i++) {
UILabel *label = [[UILabel alloc] init];
float x = (defWidth * (float)i);
label.frame = CGRectMake(x, 0.0f, defWidth, defHeight);
label.backgroundColor = bgColor[i];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"AppleGothic" size:20];
NSLog(#"%d,%f", i, x);
label.text = [NSString stringWithFormat:#"hogehoge%d", (i + 1)];
NSLog(#"%#", label.text);
[_scrollView addSubview:label];
}
}
But I cannot put images instead of labels. How can I put images in the scroll view so that it will be a slide show of few pictures?
to create a sliding image show so the user can swipe through the the images or images of the tutorials as you say, i find ray's tutorial very helpful. it also comes with the project sample code that has four different ways of sliding images. here is the link for that tutorial which is going to help you understand how it can be done.
http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
in adition if you want to just have a slide show there is a great control done by kirualex that is a nifty control and you can download it here:
https://github.com/kirualex/KASlideShow
i could have done a run down of these methods but it would be very lengthy and i think by going through the tutorial and looking at the codes you will understand this subject much better.

"Stealing" the UIPanGestureRecognizer from UIScrollView

I've got a UIScrollView (A) parent on the screen, inside it's content I have two controls -
another UIScrollView (B) at the Top an an UIView (C) at the bottom,
A is full screen (460px)
B 460px but content is longer then the screen (600px) so it has it's scrolling inside
C 460px fixed
also paging is enabled so B is the 1st page and C is the 2nd,
When I pan down B is scrolling and when it reaches the bottom it's bounces instead of pulling view C, if I set the bounce to NO then it's stuck at the bottom and only if I raise the finger and pan again it pulls the view C..
I saw some related questions but non of them helped me (How to steal touches from UIScrollView?)
a code sample to recreate the situation
(or download from my dropbox https://www.dropbox.com/s/f9j0vkg902214ab/Test2.zip)
- (void)viewDidLoad{
[super viewDidLoad];
// create main scroll
UIScrollView *scrollA = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollA.pagingEnabled = YES;
scrollA.bounces = YES;
[self.view addSubview:scrollA];
// create top scroll B
UIScrollView *scrollB = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollB.backgroundColor = [UIColor greenColor];
scrollB.bounces = YES;
[scrollA addSubview:scrollB];
// create something to put in B
CGRect frameViewB = scrollB.frame;
frameViewB.origin.x = 30;
frameViewB.size.width = 260;
frameViewB.size.height = 600;
UIView *viewInsideB = [[UIView alloc] initWithFrame:frameViewB];
viewInsideB.backgroundColor = [UIColor blueColor];
[scrollB addSubview:viewInsideB];
[scrollB setContentSize:viewInsideB.frame.size];
// create bottom view
CGRect frameC = self.view.frame;
frameC.origin.y = 460;
UIView *viewC = [[UIView alloc] initWithFrame:frameC];
viewC.backgroundColor = [UIColor yellowColor];
[scrollA addSubview:viewC];
// set content for 2 pages
[scrollA setContentSize:CGSizeMake(320, 920)];}
Thanks

Adding the scrollview created by photoscroller to a subview

I'm trying to modify Apple's PhotoScroller example to make the scrollview that is created into a subview instead of it being a view that takes up the entire screen. Any ideas on how this can be accomplished?
- (void)loadView
{
// Step 1: make the outer paging scroll view
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
pagingScrollView.delegate = self;
// When I do this it fails
[self.view addSubview:pagingScrollView];
// Step 2: prepare to tile content
recycledPages = [[NSMutableSet alloc] init];
visiblePages = [[NSMutableSet alloc] init];
[self tilePages];
}
You just need to modify the frame of the scrollview to be positioned and sized how you want:
This is the line in the view controller that sets it up in the example
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
As an example here is a sample frame with some hardcoded values:
CGRect scrollFrame = CGRectMake(100,100,100,100);
pagingScrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
So, I found out that I was able to add the scrollView as a subview by changing the method from loadView to viewDidLoad.
I have no clue why that works, but it does. I'd love to know why that's the case however...

Creating a UISearchBar and UITableView inside a UIView

Disclaimer: I'm something of an iOS n00b. I have a navigation-based app -- i.e., in my AppDelegate, I create the nav frame:
self.navigation_controller = [[UINavigationController alloc] initWithRootViewController:home_view_controller];
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.window.rootViewController = navigation_controller;
self.window.makeKeyAndVisible;
Inside the frame of my home view, I want a search bar, then the scrollable table view. So, I wrote this:
- viewDidLoad{
(HomeView*)home_view = [[HomeView alloc] init];
self.view = home_view
self.view.backgroundColor = [UIColor whiteColor]
(UITableView*)self.table_view = [UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.table_view.bounds.origin.y += 44;
self.table_view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.table_view.dataSource = self;
self.table_view.delegate = self;
[self.view addSubview:self.table_view];
search_frame = self.view.bounds;
search_frame.origin.y = 48.0;
search_frame.size.height = 48.0;
self.search_bar = [[UISearchBar alloc] initWithFrame:search_frame)];
[self.view addSubview:self.search_bar];
}
The table view displays fine, but occupies all the space below the navigation bar. I want to have the navigation bar, then immediately below it the search bar, followed by the table view. At the point of viewDidLoad, the UITableView does not yet have a non-zero bounding rect and I expect that's part of the problem. Additionally, I've specified UIViewAutoresizingFlexibleHeight in my autoresizingMask when I really want it glued against the search bar -- again, I believe this may be part of the problem.
What have I misunderstood here?
Thanks
Steve,
Try following changes:
1) Set tableview's frame using:
CGRect targetRect = CGRectMake(0, 48, self.view.frame.size.width, self.view.frame.size.heigth);
(UITableView*)self.table_view = [UITableView alloc] initWithFrame:targetRect style:UITableViewStylePlain];
2) Add following autosize mask also:
// This value will anchor your table view with top margin
UIViewAutoresizingFlexibleBottomMargin
3) Set searchbar's frame to following rect frame:
CGRecttMake(0, 0, self.view.frame.size.width, 48);
What I would do, is just add the UISearchBar as the first UITableView header.
(method: tableView:viewForHeaderInSection:)
Check this blog post:
http://jainmarket.blogspot.com/2009/08/add-searchbar-as-uitableviews-header.html

Resources