UIScrollView is not scrolling the content of another subview - ios

Hi everyone. I dragged a UIScrollView to my storyboard, then I added a UIView on top of that UIScrollView. This UIView loads another subview dynamically depending on what the server throws to this app.
The problem is the UIScrollView is not scrolling the content, even though I made the loaded UIView bigger than the UIScrollView. I'm not invoking [myScroller addSubView:myUiView]. Am I doing it right? Thanks in advance!

For every time you change the content try calling
//removeFromSuperview the UIView was set as content view
self.myScroller.contentSize = self.myUiView.frame.size;
[myScroller addSubView:myUiView];
Don't forget to link both views on Interface Builder
PS: In this apporach the subView must not be inside the scrollView on IB

You must set the content size of the scroll view.

Add this in your view controller.
- (void) viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
myScroller.contentSize = CGSizeMake(CGRectGetMaxX(myUiView.frame), CGRectGetMaxY(myUiView.frame));
}

Related

The content exceeds the ViewController view, How could i show my lengthy contents in screen by scrolling?

I am having more lines of contents which exceeds the ViewController height, I want to show it in same screen by scrolling. Is there any way to achieve that?
Use a UIScrollView and put your contents in it.
You can use UIScrollView or UITableView for the same.
Add all views as subviews of one super view say containerView. Add containerView as sub-view of UIScrollView. Do not forget set appropriate content size of UIScrollViiew. You can do it by proper autolayout or programatically as
CGRect contentRect = CGRectZero;
for (UIView *view in self.scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
self.scrollView.contentSize = contentRect.size;
You can also use UITableViewController with static cells. You can add view in static cells and you are good to go. UITableViewController itself manages scrolling.
Hope it helps. Happy Coding!!
You can use a UIScrollView (As #luckyShubhra and #chedabo suggested) , you can setup the UI correctly in Storyboard in this case and have the contentsize of the UIScrollView set to the size of the viewController contained in the UIScrollView...
Then also the UITableView (As #luckyShubhra suggested)... if your data is setup to deal with as a list of NSStrings or object, this can work well...
Then if the content is a NSString, and you would use a UILabel, I would suggest changing it to a UITextView... This will make it possible to scroll as well. If the contents is not to be edited just change the Selectable and Editable booleans to false...
UIScrollView or even UITextView can help that too. if it is HTML, you may consider of rendering it with UIWebView.
just make sure the control height is lesser than the view controller, then it will render the contents in a scrollable manner.

iOS Storyboard: how do you edit a content view larger than screen in a UIScrollView

I have a view that is larger than screen, I need to put it in a UIScrollView.
So I first add an UIViewController to story board, then, I add a UIScrollView to the root view of my view controller, then when I add subviews of UIScrollView, but I can't add them outside the scrollview area I can see, how to solve this problem?
Change the simulated size of the view controller to Freeform and then you can set the size to whatever you want. After editing, you may want to set it back.
Edit 1:
1) put your subview into scrollview's visible area
2) change frame of the new subview as you wish
3) change contentSize property of scrollview with runtime attributes
Edit 2:
u can create a new view with xib, which contains all your subviews, and then add this view on scrollview OR u can use storyboard's Container View like this:
p.s.: don't forget about contentSize (point 3 in my first edit), but if you are using auto-layout, you need to set it programmatically like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_scrollView.contentSize = CGSizeMake(320, 250);
}
put a scroll view in the view controller ,and put a view inside the scroll view named containerView.
design the big content in another view controller (named bigContentView)
in code ,view load event ,
a. get the bigContentView by storyboard Id. ( get the controller and use controller.view)
b. create the outlet to the containerView.
c. update the containerView's frame according your requirement.(update the width and height)
d. bigContentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
e. containerView addSubView:bigContentView.

Horizontal dragging of UIScrollView

I have a subclass of UIScrollView class and this scroll have vertical content. But I need drag and drop this UIScrollView in the parent view on horizontal direction. How can I implement this?
You must have a specific hierarchy:
Before you do anything you must go to your StoryBored and deselect the Autolayout option under the FileInspector.
View
ScrollView
ContainerView (if you want one or have one)
View
UIControl (if you have one)
THE CONTENTS OF YOUR VIEW.
You must however make a declaration of the scrollView in your header file:
IBOutlet UIScrollView *yourScrollViewName;
In your main under viewDidLoad:
-(void)viewDidLoad {
yourScrollViewName.translatesAutoresizingMaskIntoConstraints = NO;
[yourScrollViewName setScrollEnabled:YES];
[yourScrollViewName setContentSize:CGSizeMake(320 ,554)]; //320 is the x which is the width. change this to make it horizontal.
//554 is the Height this has to be larger that the screen size in order to have vertical scrolling.
[yourScrollViewName setPagingEnabled:NO];
}

IOS-UIView and UIScrollview

Dear all,
i have UIView and UIScrollView inside UIViewController.
UIView and UIScrollView has same width, width equal 500,
I need UIView to scroll when I scroll the UIScrollView. How can I achieve this?
P.S: UIView is not a subview of UIScrollView
Thank you for answers!
If i understand correctly u need to add into ur viewControler a horizontalscroll view and then into that view and into that u add ur view and another scrollview.
I hope i understand ur question correctly.
you need to make your UIView as the subview of UIScrollView then only you will be able to scroll the UIView when you scroll your scrollview.

Subview access to superview

I have a scrollView with its content view and finally a subview on the content view.
I would like to stop scrolling [scrollView scrollEnabled:NO] from the subview. Can I get at the method?
I don't have Xcode in front of me, but can't you get at the superview with self.superview from your subview code?
For instance, like so:
[subview.superview scrollEnabled:NO];
To stop scrolling [scrollView scrollEnabled:NO] from the subview? Maybe you need a viewController which control the views. Do you clear about the MVC?

Resources