Can't add a UIScrollView to a part of the view - ios

Inside a UIViewController, I need to have the bottom half scrollable. So I added a UIScrollView and positioned it halfway down the view's height. And in the viewDidAppear method, I have put the below two code lines to make it scrollable.
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);
self.scrollView.frame = self.view.frame;
This way works if the scroll view fills the entire view, I've tested. But this method didn't work for my need. The scroll view would automatically move up and take up the entire screen. I assumed it was the second line of code which causes this.
So I removed the scroll view, added two UIViews to the view controller. To the bottom view, I added the UIScrollView. And in the viewDidAppear method, I have put the same two code lines changing the second line to refer the frame of the UIView that contains the scroll view..
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);
self.scrollView.frame = self.containerView.frame;
But it wouldn't scroll either.
Can anyone please tell me how to do this correctly?
Thank you.

Dude, you keep setting the frame of the scrollView to something completely different from what you're actually trying to achieve.
If all you want to do is setup your scroll view so that it only occupies half the space then why dont you just set the frame so that the height only covers the portion of the screen that you want it to cover; and then set the x & y coordinates so that you draw the scroll view from the right position.
Do something like this:
//Shortcut to view's frame.
CGRect viewsFrame = self.view.frame;
/**
CGRectMake takes 4 parameters: x, y, width, height
x: is set to 0 since you want the scrollview to start from the left with no margin
y: you want the y position to start half way, so we grab the view's height and divide by 2
width: you want your scrollview to span from left to right, so simply grab the view's width
height: you want your scrollview's height to be half of your screen height, so get view's height and divide by 2.
*/
CGRect frameForSV = CGRectMake(0, viewsFrame.size.height/2, viewsFrame.size.width, viewsFrame.size.height/2);
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:frameForSV];
[self.view addSubview:myScrollView];
Then set your content size not based on an ansolute value, its best to have it based on the size of the content that's actually inside your scrollview so that your scrollview always scrolls to cover all your content inside it.
Also, remember that your scrollview will only scroll if the contentsize is greater than the scrollview's frame
UPDATE 1 after reading your comment in this post simply comment out any code in your viewController.m file related to your scrollview since youre setting up everything in interface builder.
This is the result:

Related

UIScrollView Bounces To Either Top Or Bottom

I have a UIScrollView for which I have a UIView which is the subview of the scroll view , the UIView has a lot of other subviews and I am getting the height for it dynamically after adding the subviews , this is my piece of code to add the view to scroll view
CGRect frameOfView = CGRectMake(0, 0,Get_Bounds.width, globalYPosition);
self.parentProductDetailView = [[UIView alloc]initWithFrame:frameOfView];
I am first initialising the view this way and then after adding all subviews I am doing this,
frameOfView.size.height = globalYPosition;
[self.parentProductDetailView layoutSubviews];
self.parentProductDetailView.frame = frameOfView;
[self.productDetailScrollView addSubview:self.parentProductDetailView];
self.productDetailScrollView.contentSize = CGSizeMake(0, self.parentProductDetailView.frame.size.height *1);
But my scrollview does not scroll properly it either sticks to top or bottom.
Here globalYPosition is the sum of height of all subviews added to parentProductDetailView
The procedure you used seems correct. No matter the subviews your scroll view should scroll properly by simply using a larger content size then its frame size.
The scroll view may stick, snap to some points if paging is enabled which is what is happening in your case. If the content view is larger then 1.5th of the frame size then the scroll view will snap to top/bottom or left/right. If it is smaller then it will only snap to starting position.
This may be very useful for situations like having a side menu that takes a part of a screen but in your case you should simply disable the paging and scrolling should work fine.

Adding subview to scrollview

I'm trying to add a subview to a scrollview I have in my view controller:
let size:CGSize = self.view.bounds.size;
self.scrollview.contentSize.width = size.width
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, self.tableView.frame.origin.y + 130, size.width, size.height), pageMenuOptions: parameters)
self.scrollview.addSubview(pageMenu!.view)
It works to the extent that it adds it in the correct position and height I want it. But for some reason, right now it only expands to about 60% the width of the screen (I need it to be full screen).
Things I've tried
1) Setting it so self.view.frame.width
2) Setting it to the width of another full screen element.
3) Setting it to UIScreen.mainScreen().bounds
I checked the constraints of the scrollview in storyboard and it's configured to be full screen...so I'm not sure why this wont work.
This issue is related to the constraints that need to be set to the scroll view. I have answered a similar question here. Basically you need to specify a constraint for the scroll view's content view's width. See my answer in above link for a detailed description. The problem is that the scrollview adjusts its size to its content view's size even after we provide proper constraints to the scroll view. So we need to specify the constraints of the content view of the scroll view with respect to the scrollview and its superview so that the content in the scrollview fits our requirement.
One thing I noticed was that in your CGRectMake code you are specifying your y origin to be tableViews y value + 130. That seems like the problem to me.

problems with scrollview, not scrolling full length

I have a scroll view, added in the IB, and also I added a lot of objects/controls to it on IB itself. Some of them are added to the scrollview but they are out of its bounds.
Back in the class, I'm setting its content size like this
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height + 500);
The scrolling length I observe is not that much. I just observe like 500 px (while the height I set in the content size is around 900 px).
Please tell me what I'm doing wrong.
I use the IB to set the scroll view and the inner controls and I let the scroll view to be longer than the view area it self as why do you need to set it inside the code unless you draw the inner controls dynamically at the runtime ...I hope it is useful

Scrolling Issues in IOS

Scroll view did not set content size if I set it using a dynamic variable i.e.
scrollView.contentSize=CGSizeMake(320,scrollView.frame.origin.y+120);
the scroll view size remains same after calling above method.
But if I set it using a number (integer or float) it gets changed but it gives a very strange effect on scrolling. Picture of effect is attached.
scrollView.contentSize=CGSizeMake(320,1000);
This is not allowing me to post image anyways a strange effect appears.
How I can get rid of it?
Please tell Whats going wrong or what I am missing?
Now i have got rannking upto 11 the image for effect is given here.
Before Increasing Content Size:
After Increasing Content Size on Scrolling follwing effect appears:
Select the Scroll View and view its Size Inspector window. Observe that its size is 320 , 713 points (in my case). If you do not see the same
size as what I have, this is a good time to adjust the size so that it is the same as mine. You will
need to use this value in your code
(void)viewDidLoad {
scrollView.frame = CGRectMake(0, 0, 320, 460);
[scrollView setContentSize:CGSizeMake(320, 713)];
[super viewDidLoad];
}
i think you should use frame.height to achieve your goal
the content size must be greater then frame size of scroll view for scroll
and you are using y origin of scroll view so may be that's the problem.
scrollView.contentSize=CGSizeMake(320,scrollView.frame.size.height+120);
In this line:
scrollView.contentSize=CGSizeMake(320,scrollView.frame.origin.y+120);
You are setting the scroll view's contentsize's height to scrollView.frame.origin.y+120.
Means, if your scroll view's frame is x=40, y=40, width = 500, height = 500. Then the scrollView.frame.origin.y+120 will be equal to 40 + 120 = 160. Which is less than your scrollview's height. So your scroll view won't scroll to up or down.
In the second line:
scrollView.contentSize=CGSizeMake(320,1000);
You are scroll view's content size's height to 1000. That is greater than the scroll view's height so it'll scroll to up and down.
Finally.......
The issue was scroll view increases width of scroll view indicator which shows on the screnn while scrolling. You have two options to get rid of this.
Disable vertical or horizotnal indicators which one causing problem, but this may cause some problem with scroll view content size in my case it caused a problem last cell was not being viewed.
2.Let it appear but make it out of screen by the following code snippet.
[myScrollView setScrollIndicatorInsets:UIEdgeInsetsMake(0.0, 0.0, 0, 320.0)];
this is working perfectly but scroll indicator is gone out of screen.

UIScrollView content size clipping subviews when it shouldn't

I have a UIScrollView that contains several dynamically resizing subviews. I can resize and layout the subviews just fine, but when I set the content size of the scroll view itself, the bottom subviews are clipped. Is there some reason why a scroll view's content size height should be larger than the sum of the heights of the views it contains?
Here's my situation in more detail:
I have a superview containing a UIScrollView containing several subviews. In the superview's layoutSubviews method, I calculated the needed size of each subview, then set the frames so the subviews are tiled vertically down the screen with a bit of space between them. When done, I set the height of the UIScrollView's content size to be the end of the last subview (origin.y + size.height). In theory, this means the bottom of the scroll view's content area should exactly line up with the bottom of the last subview.
But it doesn't. Instead, a nice chunk of the last subview is clipped. It's still there - if I scroll down I can see the remaining portion during the "bounce". The problem is even worse in landscape mode - a much larger portion of the bottom subview simply isn't visible.
The subviews are all being arranged and positioned properly. The problem is that the UIScrollView's contentSize seems to need to be significantly larger than the sum of the heights of the subviews (plus the space between them). This doesn't make any sense to me. Furthermore, the amount the size is "off" varies - I reuse this view several times with different subviews, and they're all off by a different amount. Therefore, simply adding a constant to the content view height won't help.
What is causing the content size (or my height calculations) to not function correctly?
Code:
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat width = self.bounds.size.width - [self subviewLeftMargin] - [self subviewRightMargin]; // All subviews have same width as parent view
CGFloat x = [self subviewLeftMargin]; // All subviews should start at the far left of the view
CGFloat y = [self spaceBetweenSubviews]; // Running tally of the y coordinate for the next view
/* Adjust the subviews */
for(UIView *view in self.theVariousSubviews) {
/* Resize the view with the desired width, then let it size its height as needed */
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, width, view.frame.size.height);
CGSize newSize = [view sizeThatFits:view.frame.size];
/* Set the origin */
//The subviews are positioned correctly, so this doesn't seem to be a problem
view.frame = CGRectMake(x, y, newSize.width, newSize.height);
/* Have the view refresh its own layout */
[view setNeedsLayout];
/* Update the y value for the next subview */
y += newSize.height + [self spaceBetweenSubviews];
}
/* Resize the scroll view to ensure it fits all of the content */
CGFloat scrollViewHeight = y;
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, scrollViewHeight);
//Content size is set to the same total height of all subviews and spacing, yet it is too small. Why?
}
hi it seems to me that your calculation and resizing timing is wrong.
Without the missing code for the layout change I could not fully understand the problem.
What strikes me is that you are assigning view.frame twice and between the new calculation you intercept the process with sublayouting which might change some of the values your calculation is depending on.
I could only advice you to separate the calculation from layouting and not invoke methods while you are calculating. To bring light into it you should either drop a sample app with the missing calculation or for yourself add some NSLog statement showing you the frame origin size of any subview and the contentOffset for the scrollview.
On my experiences the scrollview is working properly in general so I would expect a bug within your code.

Resources