Adding subview to scrollview - ios

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.

Related

how to resize scroll view after remove subview swift?

I have subview in a scrollview and after I remove some subview from the bottom there is a blank space and the scroll view still keeps height, not resizing. How to fix this?
Scrollview's contentHeight & contentWidth is calculated from its subviews(if used auto layout). If autolayout is not used then you need to set its content size.
So whenever you are adding or removing subviews in scrollview you need to take care of its contentHeight and contentWidth.
If you're using auto layout, you need to add constraints to newly added view in such a way that scroll view can calculate its contentHeight from it.(in auto layouts you don't need to explicitly set the content height to scroll view)
If you're using frame base layout, you need to set the contentHeight explicitly to the scroll view. In frame base layout, you need to take care of scrollviews content size.
Why is there blank space in scrollview?
When a subview is removed from scrollview, its content size need to be adjusted. It means it is not able to calculate its content size.
you can use this line
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: (YOURVIEW) - (THEREMOVEDVIEW))
I recommend you that you must save the height before remove view for later you can adjust.
UIScrollView doesn't know the height of its content automatically. You must calculate the height and width for yourself like below:
scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 700)
note: change width and height as you want.
you can try this also:
let content: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in
rect = rect.union(view.frame)
}
scrollView.contentSize = content.size

UIView Resize for content

Couldn't find any answer for this, or am I missing something...
In iOS with autolayout on storyboard. If I have a UIView with some controls in it, depending on what the user clicks, the subviews are resizing. Can I somehow have the superview resize to the content?
In my app I have a view, but if a button is clicked subview is hidden I want the superviews height to decrease to exclude the hidden view. I can hook up the height constraint and handle it programatically, but it would be nice to handle it automatically....
Is there a way?
Can I assume that you have some constraint that relates the size of your superview to the size of your subview?
If so, all you will need to do to resize your superview when you hide the subview, is change the height constraint of your subview to 0 (auto layout won't care if your subview is hidden or not, it only takes into account constraints).
you can try something like this:
You can try putting this code to your buttonAction
yourView.frame = CGRect(x: anotherView.bounds.minX, y: anotherView.bounds.min, width: anotherView.bounds.width , height: anotherView.bounds.height)
in this case "yourView" will change its size to "anotherView".
yourView.frame = CGRect(x: anotherView.bounds.minX, y: anotherView.bounds.min, width: anotherView.bounds.width , height: anotherView.bounds.height / 2)
This example will change the height to 1/2 of "anotherView" frame.
Hope it will help you a little bit.
I was missing something.. 😄
What I didn't get was that the view will resize for its contents if you don't set a height for it and have the content have a constraint for the bottom of the view.
So I ended up setting the height of the hidden control to 0, and the bottom control constraint to parent bottom.

How to configure view width fixed width and variable height with AutoLayout

I have the following setup: a UIView containing a UILabel and a UIButton. The UIButton has fixed dimensions (it doesn't really matter). The UILabel, however, is constrained by the view's bounds.
I want to be able to set the UIView's width and it should automatically resize itself so that its height allows the whole label content to be visible.
I have tried the following:
self.view.frame = CGRectMake(0, 0, desiredWidth, 0);
CGSize fittingSize = [self.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
self.view.frame = CGRectMake(0, 0, fittingSize.width, fittingSize.height);
But this changes both the height and the width of the view, not keeping the width at the desired value.
Is there an elegant method to do this?
Thanks in advance for any help?
In AutoLayout, you would need add a width, top, bottom, leading (left) and/or trailing (right) constraints for the view on to the superview and declare properties for at least the top and/or bottom constraints. After adding the aforementioned constraints you could then change the height of the view by adjusting top and/or bottom constraints constant value.
Here is a link to the Auto Layout Guide.
Auto Layout Guide: Introduction

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

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:

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.

Resources