UIScrollview is not scrolling - ios

I know there are tens of similar question about scrollview but I have tried everything, Scrollview is not scrolling.
First I created a uiviewcontroller with a xib file, and set it to freeform and arrange the size of it and added a scrollview on view.
Then added it as a childviewcontroller to another view controller.
-(void)scrollViewSetup
{
if (!_scrollView) {
_scrollView=[[UIView alloc] initWithFrame:CGRectMake(0,200,self.scrollView.frame.size.width,200)];
[self.view addSubview:_scrollView];
ScrollView *displayPortf=[[ScrollView alloc] init];
//add as a child view controller here
//displayPortf.delegate=(id)self;
[_scrollView addSubview:displayPortf.view];
[displayPortf didMoveToParentViewController:self];
[self addChildViewController:displayPortf];
}
}
Content size of scroll view is (768,200) on iPad.
I tried setting content size to (1920,200) in viewDidLoad didn't work then viewDidAppear didn't work then viewDidLayoutSubviews didn't work.
Then I checked the use auto layout box and tried to add constants by goingeditor->resolve auto layout issues->add constraints
How can I make scrollview to scroll?
Note: Parent view uses auto layout and storyboard.

I guess that the reason of issue is zero width of scrollView.
When you call this:
_scrollView=[[UIView alloc] initWithFrame:CGRectMake(0,200,self.scrollView.frame.size.width,200)];
_scrollView is initiated with width self.scrollView.frame.size.width, but at this moment self.scrollView is nil as it related to instance variable _scrollView that hasn't initiated yet.
Try to set some non zero float value for view width:
_scrollView=[[UIView alloc] initWithFrame:CGRectMake(0,200,768,200)];

Related

Add UIScrollView programmatically in a ViewController that contains a smaller UIView with objects

I have an UIViewcontroller. I have a smaller UIVIew in this UIVIewController. Now i am trying to add an UIScrollView programmatically that should contain the smaller UIView so i can scroll the smaller view up and down. The smaller UIView is already created using the interface builder of xcode. So now i use the following code to add the scrollView and then add the smaller View in the scrollView. The scrollView should have the same size in width as the smaller View.
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.smallerView.frame.origin.x, self.smallerView.frame.origin.y, self.smallerView.frame.size.width, self.smallerView.frame.size.height)];
[self.scrollView setContentSize:CGSizeMake(self.smallerView.bounds.size.width, self.smallerView.bounds.size.height*1.2)];
[self.scrollView addSubview:self.smallerView];
[self.view addSubview:self.scrollView];
The weird thing is that the objects that are contained in the smaller UIView are not inside the bounds of the UIScrollView. What am i missing? Do i have to add constraints for the smaller UIView programmatically? Any help appreciated.

How to rebuild view loaded from nib into new frame size

I'm trying to attach a view loaded from a nib file into another view.
let say first view is subView and the second is containerView.
When I added the subView to container view using addSubView method, the subview has its content size and so appears truncated.
when I try to set the frame of the subview to containerView frame, it takes the frame but this brake the autolayout in other words the subView constraints got meaningless.
How can I rebuild the subView in its new frame.
Here's how I do it, using 2 UIViewControllers, a container vc and a content vc. This method is in the container view controller:
- (void) displayContentController:(UIViewController*)contentViewController
{
[self addChildViewController:contentViewController];
contentViewController.view.frame = self.view.frame;
[self.view addSubview:contentViewController.view];
[contentViewController didMoveToParentViewController:self];
}
In my app, autolayout for the content works perfectly in all orientations and size classes.

Resize contentSize of UIScrollView with embedded view controller

I have a UIViewController (MasterViewController) with a UIScrollView, that has a header view on top, and a container below.
When I embed any childViewController (e.g. the EmbeddedViewController), the contentSize of the UIScrollView obviously needs to be adjusted. In order to adjust the size, I need to wait until the embedded view is layouted correctly and then set
scrollView.contentSize.height = embeddedViewController.tableView.contentSize.height + HeaderView.frame.height;
When (in the MasterViewController) can I update the scrollView.contentSize after embedding any embeddedViewController?
I thought I could call
[embeddedViewController layoutIfNeeded];
... right before I embed it into the masterView, but somehow the scrollView contentSize is not set correctly. Do you have any ideas or a best practice for dynamic length containerViews in UIScrollViews?
give scrollview contentsize in viewDidLayoutSubviews method
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width,350);
}

Resize Parent view dynamically when Child view is added out of bound

UIView *parentView = [[UIView alloc]initWithFrame:[CGRectMake(0,0,100,100)]];
UIView *childView = [[UIView alloc]initWithFrame:[CGRectMake(0,120,100,100)]];
[parentView addSubview:childView];
The code above stating childView is added into parentView at a location outside of parentView bound. Is there anyway i can resize parentView so that the childView is no longer out of bound? I want to do this dynamically. Is there any auto function available, or I have to do it manually by calculating the overall size.
Another question is, before the resizing of parentView to fit in childView, it seems that the childView is added at (0,100) instead of (0,120) of parentView, even though I set [parentView setClipsToBounds:NO];
you can resize the parent view according to the child view frame
UIView *parentView = [[UIView alloc]initWithFrame:[CGRectMake(0,0,100,100)]];
UIView *childView = [[UIView alloc]initWithFrame:[CGRectMake(0,0,0,120)]];
[parentView addSubview:childView];
parentView.frame = CGRectMake(0,0,childView.frame.size.width,childView.frame.size.height);
If AutoLayout is an option for you I would recommend looking into intrinsic content size. You can set the constraints so that the parent view will resize according to the content (childView) in Interface Builder. There is a very good tutorial on AutoLayout on Ray Wenderlich
You can create a custom UIView and override the addSubView method, in your addSubView override implementation you can check the size of the subview being added if needed and resize the current view.
#implementation CustomView
- (void)addSubview:(UIView *)view {
if(!CGSizeEqualToSize(self.view.size, view.size)) { // Sizes don't match
// resize self.view here
}
}
#end

UIScrollView Implementation

Using Storyboards, I have a UITabBarController with a UIScrollView inside, with a UIView inside the scrollView. My view is very large, and does not completely fit in the storyboard. I am not able to scroll all the way to the bottom of the View.
If you was building all of this from scratch, how would you go about it?
Say you have a UITabBarController with 2 views. hence you have two View Controllers. Lets call them 'A' and 'B'. Configuring two views within a tab bar controller shouldn't be too difficult if you refer to this link. You can achieve this using Storyboard too.
Now say, in view 'A' you want your large UIView within the UIScrollView. What you do is declare a UIScrollView with a framesize that is equal to the screen's dimensions but set the contentSize to your large size that you wanted. You can now have you large view within this scroll view.
Example:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 520)]; // little less than 568 to accommodate the tabBar
[scrollView setContentSize:CGSizeMake(320, 1500)];
UIView *largeView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 320, 100)];
[scrollView addSubview:largeView];
[self.view addSubview:scrollView];
You can do the above in Storyboard as well but i prefer using code for such tasks.
Hope that helped. Let me know if you need more help.
Did you set contentSize for your scroll view? You should to set contentSize for scroll view to size of content view. For example if you want to show view in scrollView you should do something like this:
[scrollView addSubview:view]
scrollView.contentSize == view.bounds.size
I was able to figure it out. What I ended up doing was creating a new Storyboard with a tabView already implemented. Added a new scrollView to the storyboard tabView then copied my original UIView, that I wanted to scroll in, into the scrollView.
I draged the new UIScrollView out into my .h file and created a new property.
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
Then I set the new property up like so and viola!
[super viewDidLoad];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(320, 800)];

Resources