UIScrollView not scrollable vertically with storyboard(using autolayout)? - ios

I just add a scroll view to the storyboard, and added two views on the top and bottom, and making the bottom some part outside of the view to make it scrollable, but it doesn't work, what I have done wrong?

Inside you scrollview add a view with fixed height constrains and inside that view you can add your subview according to your design.
You can also set content size of the scrollview from storyboard.

If you are not using AutoLayout, then try to set scrollview.contentSize = bottomview.frame.origin.y+bottomview.frame.size.height This will make scrollview scrollable when device screen height is less than bottomViews bottom edge. In this make sure that your bottomview us immediate subview of scrollview.

For scroll in scrollview you need to give inner bound to scrollview with subview.
See image vertical scrolling with auto layout
Your problem may be you not give top and bottom inner bound to scrollview.
Like you added 2 view then for top view give top constraint and for bottom view give bottom constraints to scroll view.
Now you subviews height must be more then scrollview's height.
Scrollview need inter bounds (scrollview to it's subview) to scroll for vertical (top and bottom) and for horizontal (left and right).

Related

Scrollview in storyboard does not scroll

I created a scrollview in storyboard with multiple views but the scrollview does not scroll. See the screenshot. I have a scrollview with images and another view embedded that spans outside the view area. I would like the scroll view to scroll down but it does not when I see the code in the simulator.
You need to add a UIView with 1000(or whatever you want) height constant to scrollView and make the UIView equal width to view
ScrollView needs to know its scrollable area, so you need to provide information about width and height for ScrollView's content:
width - you can create empty view (with height constraint equal to 1), place it inside scrollView, set its leading and trailing constraints to scrollView and set width constraint equal to main view. Then, scrollView will know that its scrollable area has the same width as screen.
height - you need to provide top and bottom constraints for first and last components inside scrollView (and all components should have specified height). I guess you forgot about setting bottom constraint for the last item.

iOS - A view controller uiview having horizontal and vertical scrolling

I am looking for some guidance on creating a view controller with uiview and scroll view that allows horizontal as well as vertical scrolling. My uiview needs to be wider and taller than the width of the iPhone so that it holds more content in both directions.
I tried by modifying the view controller property to freeform and increasing its width and height. This does allow scrolling to happen vertical and hold more content but cannot achieve the same in horizontal direction.
I want to create a map of a building that someone could scroll in both directions to view it completely.
Is it possible, if so how?
Thanks.
It depends on the Content size of ScrollView, not on the size of ViewController in the Interface builder (Storyboard).
Try to add a view inside Scrollview and add Top, Bottom, Leading, Trailing constraints with that view to Superview. Along with that add width and height constraints to the view (that's added in the scroll view) and make sure that width and height constraint value is greater than the size of Window (iPhone screen).
For detail refer this link https://developer.apple.com/library/archive/documentation/WindowsViews/Conceptual/UIScrollView_pg/CreatingBasicScrollViews/CreatingBasicScrollViews.html#//apple_ref/doc/uid/TP40008179-CH101-SW2

Setting UIScrollView Width

I have a UIScrollView added to my ViewController. And a View on top of that Scrollview. I have done the following:
Placed scroll view inside my original View and set top, left, right, and bottom constraints. Unchecking Constrain to margins.
Added a UIView within the scrollView (to hold my labels and such) and added the top, left, right, and bottom constraints, constrain to margins unchecked. And set equal widths to the original View
I then add an image view and three labels inside the view placed within the scroll view. And add top, left, right, bottom, and height constraints for them.
The scroll view works and my view does scroll and my labels and image view are centered but everything is very wide.
I am wondering how I make it so the View is not wide and I cannot scroll horizontally, only vertically.
Add an equal-widths constraint between the view inside the scroll view, and the root-level view of the view controller.
I just recently figured this out. Try doing this:
Make a scroll view
Put all of your labels and buttons into a stack view (If you don't know about stack views, check this out: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStackView_Class_Reference/
Constrain the stack view's width and make sure the Allignment and Distribution are set to fill
Put the Stack View inside of the scroll view
Constrain the Scroll view to your hearts desire
Putting the labels and what you want in the scroll view should help with the width boundaries. Here's a picture of my successful scroll view. Hope this helps :)
In short, the contentsize is no effect while you use autolayout.
if you want you scrollview can scroll, uou should add a container inside scrollview and add origin subviews to the container.
For example, use snapkit (maybe some typos here):
you have a scrollview.set it's equal self.view's frame by autolayout
[import]then you add a containerView as scrollview's subview. set contraints:
make.edges.equalTo(scrollview) // mean its frame equal to scrollview
make.width.equalTo(self.view) // set width equal to self.view
make.height.equalTo(yourheight).priorityLow() // the actual height,if no changes,you can ingore .priorityLow()
add your sunviews to containerView,add the contraints must set to containerView,can't set to scrollview's.forexample,:
containerView.addSubView(myview)
myview.snp_makeConstraints { (make) -> Void in
make.left.equalTo(self.containerView)
make.top.equalTo(self.containerView)
make.right.equalTo(self.containerView)
make.height.equalTo(267)
}
Read this official doc -
Technical Note TN2154 UIScrollView And Autolayout.

UIScrollView has an unexplainable position in UITabBarController with autolayout

Using Xcode to create a new project, a Tabbed Application.
On the first view controller I add a UIScrollView, bind it to the view for all borders. Then I add a UIView "Content View" with equal width as the superview of the UIScrollView. I add a label on the top and a label on the bottom of the content view, and make sure that the size of the content view amounts to a higher height than the height of the scroll view, so that we get a scroll.
Still I get a horizontal scroll bar in the view. Why? The width of the content view is bound to the width of the superview of the scroll view, which should thus be as wide as the device, and give no scroll. What am I missing?
The project is downloadable from here if you want to play with it.
Your problem is that you have two constraints for the ScrollView which are set relative to the margin.
If you make the leading and trailing constraints relative to the superview with no margin then your code works as you require with no horizontal scroll bar.

Scrollview not scrolling subview in autolayout

I have a view controller with a scrollview.
The scrollview has a sub View (content view) which is a view I have copied from another VC. I cannot get the scroll to work, I have tried:
Set constraints of scrollview against the container (pinned top/bottom/sides =0)
Setting Horziontal and vertical spacing of Content view against the scrollview which also didn't work
Set the content view to superview Leading/Trailing space to container margin, Top space to top layout guide and Bottom Space to bottom layout guide - still no scroll
Have also tried to set the scrollview size in viewdidload :
scrollView.scrollEnabled = true;
scrollView.contentSize = contentView.frame.size;
And no scroll.
I am possibly missing something basic here but although have tried numerous tutorials on line I can't get my head around using scrollview.
Pop up when dragging content view to scroll view
Your content view is not embedded in your scroll view.
to Scroll UIScrollView you need following Steps
pin UIScrollView with its SuperView from all four side Top, Left, Bottom, Right
Add a contentView (UIView) in ScrollView and pin it with UIScrollView with 0 margin from all four sides. also add equal width constraint.
add subviews to contentView make sure all view pinned with constraint from top bottom & also add Height Constraint for subviews.
no need to set any property or content size in code.

Resources