Why is my UIView within UIScrollView not scrolling? - ios

I'm trying to setup a scroll for a UIView within a UIScrollView.
My view hierarchy is:
UIView (Controller) > UIScrollView > UIView (I'll call this subView from this point on)
I have used the IB to set the layout. I set the top of the subView to be inline with the top of the UIScrollView frame. The subView is as wide as the UIScrollView frame and extends beyond the height of the UIScrollView frame.
In the subView I have UILabels (also set up using the IB). To test the scrolling, I have one label that is just peeking above the bottom border of the UIScrollView frame. When I run the app this label would ideally be able to scroll into view, however, there is no scrolling and the label is just appearing as being cut off where the UIScrollView frame ends.
In the ViewController.m I have the following:
- (void)viewDidLoad
{
self.scrollView.contentSize = self.scrollSubView.frame.size;
}
where scrollSubView is the UIView within the UIScrollView.
What am I missing (doing incorrectly) that is preventing the scroll from working?
Thanks for any assistance offered.

Possible Issue
Autolayout is on, and it won't let scrollview work as required.
Possible Solution
Check for autolayout of storyboard and untick it.
If you dont want to set it to off, you can follow this short video
which is wonderfull to deal scrolling with autolayout. :)
UIScrollView with autolayout on

Related

UIScrollView bounces back when let go

I have a followed this answer Putting a UIScrollview in a viewcontroller (storyboard) to embed a scrollview in a viewcontroller (Mine is vertically instead of horizontally).
All the constrains are working except the fact that the view bounces back when it is let go. I have tried a lot of different solution, but none is working! Please help, I'm using autolayouts my main goal is to be able to show all the buttons on the screen through scrolling.
EDIT:
I have been trying all types of ways to increase the contentSize, but nothing happens - the scrollview won't scroll properly. I tried different codes from people with the same problem, but the bouncing won't stop. Am I doing anything wrong on my constraints? This is extremely frustrating, please help. My constraints (using autolayout) (as I said, I followed this answer Putting a UIScrollview in a viewcontroller (storyboard))
You have to increase your scrollView's contentSize to fit all of it's sub views.
You should add subview to scrollview, then add all other views to that subview.
scrollView
contentView
subview_1
subview_2
Then add constraints between scrollView and contentView (leading, trailing, top, bottom) and contentView should have width and height constraints set properly (set width equal to scrollView width)... the content size will be calculated automatically.... just set height

UIScrollView not scrolling with UItextfields

I am making a normal view where users can update their profiles. I followed these steps to make that view
Created a new UIViewController with xib file
Added a UIScrollView in super view
Added almost 9 UITextField and 1 small UIWebView
Satisfied Autolayout constraints like top UITextField will have top, left, right and height constrains and same for all the following
controls but last UITextField have top, left, right, bottom and height
constraints.
Now all the constraints are applied and satisfied but when I run the view and then try to scroll by dragging UITextField then scrollview is not scrolling but if I scroll by dragging from some area other than UITextField then it is scrolling very nice. Can anybody tell me what can be the main problem?
Note: there is no code yet other than setting up xib file.
A sample project is available on this link https://www.dropbox.com/s/7oqry8yzd9twnp1/TestScroll.zip?dl=0
Overriding UIScrollView touchesShouldCancelInContentView method will solve this problem.
According to Apple touchesShouldCancelInContentView is called before
scrolling begins if touches have already been delivered to a subview
of the scroll view. if it returns NO the touches will continue to be
delivered to the subview and scrolling will not occur.
By default this method returns NO if view is a UIControl. So the scroll doesn't happens for the UIControls.
If we returns YES from this method the touches will not be delivered to subview, So the scroll will occurs.
So override UIScrollView touchesShouldCancelInContentView like following
#interface MyScrollView : UIScrollView
#end
#implementation MyScrollView
- (BOOL)touchesShouldCancelInContentView:(UIView *)view{
return YES;
}
#end
NOTE: touchesShouldCancelInContentView method only calls if we set the canCancelContentTouches property to YES
Hope this helps.
You need to add an extra parent View for all the textfield in ScrollView.
Note:
Give constraints to UIView w.r.t. UIScrollView as Leading, Trailing, Top, Bottom and Equal Widths.
Also don't forget to add the bottom constraint for last object in UIView w.r.t. UIView (Customize it to approx. 30 since it will not
scroll much after finishing the contents).
Customize the constraint of UIView bottom w.r.t UIScrollView to 0.
See:
Set delayContentTouchesProperty to YES for UIScrollView. This would let you scroll if you touch contents of UIScrollView.
You can do it either programmatically or from storyboard.
You should use uiview as a superview on uiscrollView ,put all the textfield on uiview and make sure you have all the satisfactory constraints applied on uiview as respect of uiscrollView.

Placing a UIView under a UIScrollView with autolayout

How can I display a UIView under a UIScrollview with autolayout ?
I have a scrollview and a UIView below it(the grey box in image below).
Adding it has a footer inside the scrollview is not what I want because as you can see I have tabs that can scroll horizontally... The same view needs to be under each tabs. I programatically add child view's to my scroll view.
Is it possible to put a UIView outside the uiscrollview ? If not, how can I add it above every child view at the bottom ?
I have tried messing around with the constraints but nothing worked. I am stuck.
Thanks in advance !
Storyboard :
ScrollView constraints :
UIView constraints :
I found the solution. You need to drag a UIView above the UIScrollView and add a space to the top and bottom layout. Also, I added a leading and trailing space constraint.

Can't build UIScrollView with Autolayout properly

Can't build content inside scroll view with autolayout. Yes, I've investigated a lot, read apple technical note and some articles, I know, that content view should be constrained with scroll view's superview but can't still achieve the desired result. I expect this result:
fixed or scrollable (for small screens) portrait mode
always scrollable landscape mode, not to loose or overlap my fixed content
my button (red view) should always be at the bottom
my photo (yellow view) should always be fixed size and not to loose content
(like in my wrong landscape mode)
Making this work is actually pretty easy. All you have to do are the following steps:
Add the UIScrollView as subview of your UIViewController`
Add the UIView as a subview of the UIScrollView
Add the UIButton as a subview of the UIViewController. Do not add it as a subview of the UIScrollView if you want it to stick to the bottom.
Then add the following constraints:
Pin the UIScrollView to the top, left and right of you UIViewControllers view
Pin the UIButtonto the left, right and bottom of your UIViewControllers view
Pin the UIButton top to the UIScrollView bottom
Pin the UIImageView top, horizontalCenter and bottom to the UIScrollView
Set the UIImageView height and width constraints
Set the UIButton height constraint
And that's it. Do not add anything to the UIScrollView contentView and do not set the contentSize programmatically! The contentSize ist set automatically by Auto Layout.
Here are screenshots of the constraints and the result:

Autolayout issue iOS 6

Newbie question.
I added UIScrollView as subview to main view in Interface Builder and after that I added UILabel above UIScrollView. In landscape mode label scrolls fine (label's height more than scrollView height) but in portrait it can't be scrolled.I realize that I need to delete some scrollView autolayout constraints, but all of them have delete option in inactive state in Utilities -> Size Inspector. How can I resolve this?
You said :
"I added UILabel above UIScrollView"
That means UILabel is a subView of UIScrollView, right?
If you are using autoLayout then this is not an issue. Using autoLayout device decides frame of the UIControls itself thats why you are not able to scroll the UILabel
Get rid of autoLayout and you will be able to scroll the UILabel after setting [scrollView setContentSize:aframe];. And if due to some reason you can not leave autoLayout then you can override autoLayout after it loads using this delegate method
- (void)viewDidLayoutSubviews
{
...........Your code goes here
}

Resources