I've added a scrollview to a view in my app, the view is selected from a tab controller and as such is a container and not a UIViewController it is declared as below
class HomeViewController: Container, UIScrollViewDelegate {..
I've added the scrollview in the storyboard and added all of the components to it and I've assigned the delegate from the storyboard and I've placed this code in my ViewController
scrollview.contentSize = CGSize(width:1048, height:scrollview.frame.height)
scrollview.delegate = self
This how my ScrollView looks in my storyboard, you can see that scrollview is my UIScrollView, then I've added a UIView viewScroll and then added four views to viewScroll (View1, View2, View3 and View4), scrollview is sized at 375x340 and viewScroll is set at 1048x340
When I run the app it does not scroll. As it is not a UIViewController, do I need to approach this differently or is there something else I have missed?
I've added the scrollview in the storyboard and set the width as 1050 and height as 330
You should set the width and height of scroll view to be at most as large as its containing view. What you need to set to 1050 and 330 is contentSize of the scroll view - scrollView is only scrollable when its contentSize is larger than its bounds.
If you're setting up your scroll view using Interface Builder, then you'll need to add constraints to its children's edges. For example, in your case you'll have to add leading, trailing, top, and bottom constraints to your View Scroll view. Select it and add following constraints in interface builder:
After that there will be something like this:
When you add edge constraints to UIScrollViews child views, you are hinting the scroll view about its content size. And if scroll view knows its content size and it is larger than scroll view's bounds, you'll have scroll working.
Related
When I add a custom NSView to my NSScrollView via documentView and resize the window the content sticks always to the bottom of the window (so the scroll view is always scrollt to the bottom when the window is resized).
Is there a way to keep the scroll view scrolled to the top when the window is resized?
Edit: Uploaded reproducible example:
https://github.com/nathasmike/sample1
The autoresizing mask of the custom view is translated into constraints. To prevent this do
myCustomView.translatesAutoresizingMaskIntoConstraints = false
myScrollView.documentView = myCustomView
Add constraints to the custom view and its subviews.
Another solution is removing the autoresizing mask from the custom view in the xib.
I want to make a UIView's height equal the content that is inside of it. How do I do this? Here's my storyboard
View Controller
Scroll View
View //The view that needs to have dynamic height
UILabel
UIButton
UIView
So, I need to make the View to have dynamic height because the height of the elements inside the view change height on certain circumstances.
In my view, I added another view (named popupview) for custom popover like animation.
In the popupview, I have added a table view and have set some bounds to the table view, but the table view height doesn't change, whatever height I set in the xib.
There is another problem that when scrolling in the tableview, it scrolls out of the view even if the tableview is inserted in the popup view.
There are no issues with the image I have used. It has the same height, width as the second uiview popupview.
XIB Structure:
View1 mainView - table view
View2 PopupView {Orange View}
TableView2 - table in popup view
Try setting the "popupview" view's clipsToBounds property to YES. This should prevent the "sticking out" of the table-view, if the frame of the table-view is greater/not the same as the frame of your "popupview".
But aside from that, the proper way:
Have you experimented with the Autosizing/Autoresizing-Mask settings of your views?
Your table-view is a subview (i.e. child) of your popupview, right?
so the table-views frame should scale (i.e. autoresize) to the parrent views frame (popupview)!
You can configure this behavior either in IB in the "Size Inspector" pane of your table-view or programmatically. You should check out the View Programming Guide for IOS.
I added a UIViewController, then in the XIB i dragged a UIScrollView, and a few buttons at the bottom of the page. Then when i build and ran the application, the application scrolls but the button that i added at the extreme bottom of the page can't be seen properly. So i think i will have to set a height for the scrollview. But where in Interfacebuilder i should specify the height ?
If it can't be done in Interfacebuilder, then can someone show me how to do it programatically ?
You need to set the contentSize property of the scrollview. You cannot do it in Interface Builder. You need to set it in the code of the view controller that manages the scroll view.
scrollView.contentSize = CGSizeMake(width, height);
I'm currently trying to center a UIScrollView inside of my UIViewController class and am having a problem.
The UIViewController is inside of a tab bar and a nav bar, so it is 367 pixels tall on iPhone and 320 pixels wide.
The UIScrollView is placed inside of that UIViewController, and the UIScrollView is being loaded from another UIViewController class responsible for displaying content.
Now, my goal is to make the scrollview 10 pixels less in width than the containing UIViewController so that the scroll view does not take up the width of the screen.
However, in Interface Builder, when I select 300 as my UIScrollView width, and in my implementation file for the containing UIViewController, when I set the width as 300,
[scrollView setContentSize:CGSizeMake(300, 1500)];
the end result is a full-screen scroll view instead of one cut off by 5 at each side.
Any ideas on how to take care of this?
Thanks.
You can use uiscrollview's
#property(nonatomic) UIEdgeInsets contentInset
However, it is possible that when you are using in the class1 the scroll view from the class2, scroll view frame/content size are read from class2 object instead of class1 nib.