iOS 7.1 Why is my UIScrollView scrolling away on showing keyboard? - ios

I have a UIScrollView designed with IB.
UIView
UIScrollView
UIView
UITextField
UIButton
...
If I tap on the text field the view scrolls away towards the upper left corner of the screen before the keyboard is appearing. The space above the keyboards remains empty. I can scroll back the view if I drag in this empty space.
I have googled around, but found only postings where users want to scroll UIScrollView. I want the view to stay where it is.
Thanks in advance for any suggestions.

Here's what happened.
You designed the whole thing in Interface Builder.
The scroll view was not scrolling, so you set its contentSize in code to the size of the scroll view's primary subview (what I like to call the content view).
The scroll view was still not scrolling, so you munged the content insets - and this caused the problem that brought you here.
Your mistake was (3). Instead, you should have thought more about (2), i.e., why isn't my scroll view scrolling even though I have given it a nice big content size?
The answer is that, in a storyboard or xib that has auto layout turned on, that's not what you do. What you do is use constraints from the content view to its superview (the scroll view), on all four sides. Set the constant for all four constraints to zero. This causes the content size to match the size of the content view, automatically.

Related

Nested UIScrollviews behaving strangely

I have the following autolayout-driven setup:
Main viewController, with a scrollview inside it. Scrollview pinned to superview edges. This one scrolls up and down.
A few normal, fixed size views at the top of the scrollview
Another scrollview. This one scrolls left and right. The second scrollview contains a couple of tableviews, side by side. The idea is that the user can switch between them. They both contain a handful of cells, all the same width as the screen and 72pts tall.
The problem I'm trying to solve is that the tableview contents are not the same size. The left one has say, 6 cells, and the right one has 3.
My first approach was to dynamically change the second scrollview height to match which ever tableview was currently visible. What ended up happening was that switching between the two tableviews (by doing setContentOffset:animated:) went extremely wrong if animated was set to true - it would adjust the content offset so everything was offscreen. In fact it would set the content offset to and then as I switched, about a dozen times, then it'd reset. It was weird, I gave up.
Now I'm trying to just adjust the content inset of the main scrollview to offset the gap in the content of the current tableview, and it's also being weird. When I set the bottom content inset in viewDidLoad, it works fine. When I set it at the time the tableview becomes current, it does nothing.
What gives? What scenarios would lead to these view interactions not behaving properly?
Use different tableViewController for each table.
Embed them in pageViewController.
Add those few normal, fixed size views at the top of the pageViewControllers view.
Conform scrollViewDelegate in pageViewController.
Pass scrollViewDidScroll from tableViews to pageViewController.
Set tableViews inset to match those fixed size views at top.
Change height according to the scroll.
This is the way you can achieve the functionality you want.
I hope it helps.

Scroll bar scrolls but UIScrollView doesn't scroll

I have components in a UIScrollView. My scroll view works when it was in iOS 10 and Xcode 8 but after the update, my scroll view does not scroll. However, the scroll bar does scroll and can keep scrolling until it gets smaller and smaller. It is just the elements in the scroll view itself that doesn't scroll.
I have tried doing content size bigger than the frame size but it does not fix it. Here is my current views hierarchy.
If anyone can help me debug this, it would be greatly appreciated.
I fixed this by adding a content view to the scroll view, and then the scroll view should be the subview of main view. The hierarchy should look like this.
I encountered pretty much the same problem (scroll indicator moves and gets smaller but content doesn't move). In my case it was resolved by changing the AutoLayout constraint for the top of my content view within the UIScrollView. I had pinned it to the Frame Layout Guide rather than the top of the superview.

Stop UICollectionView hiding cells out of view bounds

I have a horizontal-scrolling UICollectionView which is nested in a UIView that is centred and occupies 80% of the screen width.
I want the UICollectionView to be visible screen edge-to-edge rather than constrained to the super UIView bounds.
I have set the following which shows the UICollectionView across the screen width:
collectionView.clipToBounds = NO
...but when dragging the collectionView, it hides cells when they are completely outside of the super UIView bounds even though they are partially visible on the screen, which leads to a weird flickering of blank space/cell.
Ideally, I'd like a way to prevent the hiding of the cells completely out of bounds. Is there a way to do this?
The UICollectionView has a maximum size of 3 cells, so I'm not particularly worried about any performance implications of having all cells visible all the time.
The only way I found is to enlarge the frame of the collection view (and its superview in your case) and add contentInset's to it. You might also want to update scrollIndicatorInsets.

Scrollview won't scroll all the way down

I have a scroll view which works fine. Almost. I'm building in Any/Any. The problem is that the scroll view won't scroll past the view controller. I have a switch that is mostly in the view controller window, but the rest is off the box (not really sure how to describe it; it's in the view in the scroll view, but the view is longer than the view controller so part of it is hidden).
The scroll view will scroll down until it hits the part where the view controller would end if you were looking at it in Xcode. There is some more stuff under the switch (labels and another switch). To view these you have to forcefully scroll down. Xcode shows no constraint errors (little red circle with white arrow).
Hopefully this makes sense
A ScrollView needs to know the height and width of the content it is holding in order to know how much to scroll and which direction. Here is a quick read on how ScrollViews work in iOS: https://www.objc.io/issues/3-views/scroll-view/
You can set this programmatically using the contentSize property, but this requires you to know and or calculate the contentSize, which is pretty tedious in most cases.
The correct way of defining the contentSize in iOS is to define AutoLayout constraints in your View. Here is an excellent tutorial on doing just that:
https://www.natashatherobot.com/ios-autolayout-scrollview/

Horizontal ScrollView inside Vertical ScrollView

I have a UIScrollView that scrolls vertically. Inside of that, I have a UIScrollView that scrolls horizontally. I will call them verticalScrollView and horizontalScrollView.
My first problem was that verticalScrollView would scroll up and down, but the horizontalScrollView would not scroll at all. After turning off Autolayout and setting the content sizes in viewDidLoad, the verticalScrollView would always immediately scroll down the bottom upon appearing on screen and only be able to scroll up a little, but the horizontalScrollView (which is at the bottom) scrolls left and right perfectly.
I have tried almost everything that I have found online, but nothing seems to allow me to scroll vertically with the Main ScrollView (verticalScrollView) and scroll horizontally with the child horizontalScrollView.
I can post any more information that may be helpful. Thank you.
UPDATE (8-15-13): I put the hortizontalScrollView into a UIView, and put that UIView into the verticalScrollView. I am now able to scroll freely in the verticalScrollView, but can only scroll one swipe left or right in the horizontalScrollView before it snapping back.
In ViewController.m, I set the content sizes.
[_verticalScrollView setContentSize:CGSizeMake(320, 1905)];
[_horizontalScrollView setContentSize:CGSizeMake(960, 190)];
Please check your Autosizing is top-left or not? May be something wrong in size inspector view in XIB.
Hope this can be helpful to you.

Resources