UIScrollView not passing touches to subviews? - ios

I have a scrollview, I embed 3 controllers inside this scrollView in storyboard, and page between them horizontally.
content size of the scrollview is (scrollViewWith * numberOfControllers , scrollViewHeight)
scrollview appears and I am able to scroll horizontally thought the views, but the views don't detect any touch. (The controllers embedded have collectionViews inside them)
I implemented touchesBegin in one of the controllers, and it's never getting called.
Any idea what could cause the scrollview not to pass touches to subviews?

This isn't necessarily an answer to why touch events aren't making it to the collection views, but it sounds like you're reimplementing UIPageViewController with the transitionStyle set to .Scroll. Maybe consider using that instead?

when adding views to a controller through embedded segue, the view of the viewController is not added directly inside of the main view, but it's embedded in another container view before being added to the main view
ViewControllerView->EmbedView->EmbededViewControllerView
So I ended up removing them both, and manually add the view of the viewControllers to the scrollView

what kinds of view's you have inside every page of the UIScrollView?
You can add anything there, for example a UICollectionView, and the only way to prevent to not pass touches inside is having the UIScrollView set as:
self.scrollView.userInteractionEnabled = NO;

Related

Is it possible to drive a table/collection view's scrolling from gestures inside another view?

Suppose there are two UIVIew subclasses in two different regions of the screen.
The first UIView subclass is a table or collectionView
The second view is a simple UIView.
Is it possible for gestures inside the second view to be "carried over" to the first view so that for example, a swipe up gesture in the second view would make the table/collection view scroll up ?
In WWDC 2012 Session 223: Enhancing User Experience with Scroll Views, Apple engineers explained that you can take a scroll view's panGestureRecognizer and add it to a different view to make that other view control the scroll view's scrolling.
Note that UITableView and UICollectionView are subclasses of UIScrollView.
So if you can put your table view and your second view into a common superview, you can move the table view's panGestureRecognizer to the superview and it will detect touches on both subviews.
[commonSuperview addGestureRecognizer:tableView.panGestureRecognizer];
The best way to achieve that is to encapsulate your 2 views in a another view and to add the GestureRecognizer on the motherView.
This way when the gesture triggers, with the location of the touch you can figure out if it started in one view or the other and you can track the move all the way to the second view.

Change constraints that are set on the parent view from within the child view

I am having the following problem over and over again and I am looking for a clean solution to do this.
I have multiple views: a parent view (my rootView, controlled by my basic UIViewController) and a descendant view (a UICollectionViewCell in a UICollectionView which is a subview of my rootView).
The size of the UICollectionView is relative to the size of my rootView: in the UIViewController of the rootView, where I created the UICollectionView, I also created constraints (between the rootView and the UICollectionView) and added them to the rootView.
When a button is tapped in the UICollectionViewCell I want to change the size of the UICollectionView. Therefore I need to alter the constraints that are set on my rootView.
I have been told that accessing the constraints of the rootView via a downcast of the UIViewController and changing them this way is dirty, because having a view talk back to its (parent)controller is wrong.
How am I supposed to act in such a situation? How can I make the controller react to the view without the view talking to the controller? A point in the right direction would be great!
After your edits, it sounds like you only have one view controller in play here. In that case, life is good. The logic to manipulate the constraints that control your collection view belongs in the view controller.
You want to pass the button taps to your view controller, and have it handle resizing the collection view.
How are you handling button taps?

Resize UIView above UITableView

I want to create layout similar to default Weather App in iOS. My view controller has UIView at top and UITableView below it. I want to resize dynamically top view while scrolling table view. I made view controller delegate of table view so I can resize top view while
scrollViewDidScroll(scrollView: UIScrollView)
is called.
The problem is that I want to disable scrolling of table view while resizing top view. Any ides? Or maybe my approach to this layout is wrong and I should use another one?
I also faced same problem as per mine problem, I used my UIView as a header of table view and used the AutoLayout in my View and if you want to disable the scrolling of your table view you can simply use a block for completion of your view resizing and you can disable scrolling of your table until your block will not give completion. Hope this solution will be helpful for you.

Embed UITableView in Scrollview

I know Apple don't recommend to Embed UITableView in UIScrollview.
So this is what I am trying to do:
I have a registration form with fields embedded in UIScrollview,
for some fields I create a drop menu by presenting UITableView, the problem is when the UITableView appears with my object the didSelectRowAtIndexPath not responding.
If there is another way it I will happy to hear, if not how can I fix it in the Current situation , thanks.
Don't add the table as a subview of the scroll view, add it as a subview of the superview of the scroll view (and, consider adding it into a container view which detects touches outside the table to dismiss it from the screen without any selection being made).
If your 'root' view for the controller is the scroll view, change your view hierarchy to use a plain view as the root and have the scroll view as a subview of that.
You may also want to use bringSubviewToFront: to ensure that the presentation is correct.

iOS prevent subview of tableview from scrolling with tableview

I have added a subview to my tableview and when ever the user scrolls the tableview, the subview scrolls with it. How do I prevent this? I know it's probably along the lines of not adding the view to the tableview's subviews, but I have no knowledge of any other ways to do this. Thanks.
If you want to make a view a subview of the table view, then you can make it floating (non-scrolling) by changing its origin.y value in the scrollViewDidScroll method.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.iv.frame = CGRectMake(self.ivOrigin.x, self.ivOrigin.y + self.tableView.bounds.origin.y, self.iv.frame.size.width, self.iv.frame.size.height);
}
In this example, "iv" is a property for an image view, and "ivOrigin" is a property for the initial origin of the image view (defined when I created the image view and its frame in viewDidLoad).
The UITableView is built and intended to be a view of things that scroll.
So, you can either fight that, which as you're discovering is quite hard since everything about the component is built and focused around scrolling and fast display of a subset of the full list data... Or, you can not fight it and put your static item on top of the table as a fixed-position item.
If there's a reason you can't add the table view and your animate-out item in your main view, you can always add a custom UIView class that contains both the table view and your animated view. Have your custom view class expose the contained table view as a .table property, and the container you're putting things in can be tweaked to use "mycontainerObject.tableview" instead of just "tableview" where needed.
Yes, it's a little more work to write the custom UIView subclass and give it a couple properties to hold the UITableView and whatever UIView you're animating out.. but it's likely a lot safer in the long run than trying to "hack" into the UITableView's methods and view hierarchy to try to give it a "fixed in place" behavior.

Resources