I have a view that has a horizontally scrolling UIScrollView. I have 4 "pages" that I want to show in my scroll view. Can I layout a huge view in Interface Builder that I can scroll across? The problem I'm having while trying to do this is that to design a xib this large across horizontally, you must set the frame of the UIScrollView to be the devices horizontal width * 4. Therefore the frame >= the content size and my scroll view does not scroll. My workaround has been to place all the elements on each of the 4 pages programmatically and adding them to my UIScrollView with addSubView: What are my other options?
You keep the frame of scroll view as horizontal width only. You should make the content size as horizontal width * 4. Now set proper frame of the views which you want to put as subviews of that scroll view. Like first subview's frame should have origin.x = horizontal width * 0, second should have origin.x = horizontal width * 1, third should have origin.x = horizontal width * 2 and fourth should have origin.x = horizontal width * 3.
This will eliminate the need of putting subviews programatically.
Freeform your xib, design your view
Related
These are my UIViews:
in my views I have 2 uitableview, 2 button.
I always check my UIView's sizes and these are the results:
randomPOC[2231:67151] MainView-1060.000000
2018-05-17 10:33:28.812877+0800 randomPOC[2231:67151] ScrollView-1060.000000
2018-05-17 10:33:28.813094+0800 randomPOC[2231:67151] ContentView-1060.000000
When my mainview , scroll and contentview have the same sizes, it doesn't scroll.
But when I divide the height into to 2 (height/2) of the mainview, it allows me to scroll. which makes me confused how and why.
when I divided the mainview height into to 2 it allows me to scroll but it doesn't covers down to the last of the tableview.
I need to resize the mainview because I need to cover the two(2) uitableview that has sometimes more than 1000height.
_mainView.frame=CGRectMake(_mainView.frame.origin.x, _mainView.frame.origin.y, _mainView.frame.size.width, uitableview2.height );
This answer has nothing to do with why your UIScrollView doesnt work.
UIScrollView will only scroll if its content size is greater than its frame size.
You should first decide the size of your UIScrollView frame, Than for a Veritical UIScrollView you should assign the content size as follow:
Lets say you have 5 UIView's which have height of 5 dp each, than you should set the UIScrollView height like so:
scrollview.setContentSize(width, 5*5).
Than if the UIScrollView frame height is greater than 25, the UIScrollView will not scroll.
Else the UIScrollView will scroll.
hope it helps!
Found the answer by using this resizing the mainview like this instead:
_mainView.frame=CGRectMake(_mainView.frame.origin.x, _mainView.frame.origin.y, _mainView.frame.size.width, [[UIScreen mainScreen]bounds].size.height );
I am facing a trouble with UIScrollView using auto layouts. I have the following view hierarchy with constraints.
ViewA (leading,trailing,top and bottom spaces to superview).
-- ViewB (leading,trailing,top and bottom spaces to superview).
--- UIScrollView (leading,trailing,top and bottom spaces to superview).
Here ViewB is adding on ViewA and i have some textfields,buttons and UITableview these all are placed inside UIScrollView.This is my view hierarchy.
Now i want to my Scrollview height increase dynamically based on tableview number of rows.
For this I'm using bellow code but it not working.Can any one please suggest me.
CGFloat height = MIN(self.view.bounds.size.height, placeTbl.contentSize.height);
self.Height_PlaceTbl.constant = height;
[self.view layoutIfNeeded];
if you want to increase your ScrollView height based on tableview number of rows you can try this
first get number of your tableview rows and then get height and width of your scrollview
let width = self.scrollview.layer.bounds.width
let height = self.scrollview.layer.bounds.height
after this , you can increase height of your scrollview like this
self.scrollview.frame = CGRectMake(0, 0, width, height + numberofrows)
I have a standard view embedded inside a scroll view and I am using Auto Layout. All is working fine, and I am almost done with it. There is just one small problem. My content view has a fixed height of 536 points, so in an iPhone 6 it is fully displayed without the need of scrolling, while in an iPhone 4S I have to scroll to view additional content.
My problem is that the view is not horizontally centered is the iPhone 6 so it displays the extra space at the bottom (I filled it with grey color for clarity), as shown in the picture below (that is the underlying scroll view).
My question is: how can I horizontally center the content view without messing up the constraints? Currently the scroll view is pinned at the four borders of the view controller and the content view is pinned to the four borders of the scroll view.
I am glad to provide any other information you need to help me solve the problem.
EDIT: This is what it worked for me
CGFloat superViewHeight = self.view.bounds.size.height;
CGFloat scrollViewHeight = self.scrollView.bounds.size.height;
if (superViewHeight > scrollViewHeight)
self.topConstraint.constant += (superViewHeight - scrollViewHeight) / 4;
You should manually adjust top constraint if your content view doesn't need scrolling.
CGFloat scrollViewHeight = CGRectGetHeight(scrollView.bounds);
CGFloat contentViewHeight = CGRectGetHeight(contentView.bounds);
if (scrollViewHeight >= contentViewHeight)
{
topConstraint.constant += ((scrollViewHeight - contentViewHeight) / 2.f);
}
I'm trying to make layout inside scrollview using this one tutorial link
And get the following result link
It will be appreciated for any advices or tutorial links. It needs only vertical scrolling
I am sure there must be other ways to do this but a quick fix is :
1.) Create a width constraint on ContentView in Storyborad.
2.) IBOutlet that widthContraint and set its value to the view frame width in viewDidLoad.
Suppose the name of the constraint outlet is contentViewWidthContraint.
contentViewWidthContraint.constant = self.view.bounds.size.width;
Another alternative to do so from Storyboard, is to fix the Contentview width to the view's width from the storyboard or to the Scrollview, if Scrollview already has a Equal width contraint with superview . Add the "Equal Width" contraint from Contentview to either self.view or to Scrollview (if scrollview, already has the width contraint)
Have you set up the "ContentView" width to match with the scroll view width? I had the same problem and I fixed with "Equal Widths".
"Equal Widths" will tell to your "ContentView" to use the same width of the "Scroll View", which should be fitting the screen if you have set up the constrain properly.
You can do this easily on the storyboard.
Drag and drop, with right click (important!!!), from "ContentView" to "ScrollView"
Release the click, you will be prompted with a menu, select "Equal Widths".
This should fix your problem using the scrollview with AutoLayout from Storyboard editor.
You can find a full tutorial how to use ScrollView with Autolayout and Storyboard here.
I hope this is useful for you :)
In the Storyboard set the width of the elements contained in your UIScrollView equal to the width of this UIScrollView (by selecting all elements and the UIScrollView holding in the panel on the left of your Storyboard and then setting the 'Equal Widths' constraint under 'Pin' on the bottom of your Storyboard). Just pinning the right sides of the elements to that of the UIScrollView won't work as it will adjust the size of its "display view" to the width of the largest element and if this is smaller than the width of the UIScrollView all elements will just appear aligned to its left side.
There is also another possibility that offers a very good result.
You can mark a checkbox:
O programmatically:
scrollView.alwaysBounceVertical = true
Try to set it's width to 0 & height equal to content size like this:
self.scrollView.contentSize = CGSizeMake(0, self.scrollView.contentSize.height);
This will work as you want. Try it & tell if still facing any issue.
For disabling the horizontal scroll, you can set the content size in the -(void)scrollViewDidScroll method.
[self.scrollView setContentOffset: CGPointMake(0, self.scrollView.contentOffset.y)];
self.scrollView.directionalLockEnabled = YES;
This is because scroll view have no idea where your content should end.
But when at least one item inside your scroll view has its "trailing space" constraint attached to a view outside the scroll view (usually a view the scroll view is sitting in or some other view of a higher level, which "knows" its width) - the scroll view will automatically get an idea about your wanted width and won't scroll horizontally (unless that trailing constraint implies having your content outside the screen).
Better if all items inside scroll view have their "trailing space" constraints connected either to each other or to a view outside the scroll view. But not the scroll view itself.
No additional code or extra constraints needed for this to work.
Too set UIScrollView constraints as like below code so it will occupied whole screen.Not exceed the screen size.
Leading Space = 0 from mainView
Top Space = 0 from mainView
Bottom Space = 0 from mainView
Trailing Space = 0 from mainView
You need to set the width of UIScrollView equal to or less than the width of your Parent View. Two ways to do it:
1) You can do this in Storyboard via layout constraints
2) You can do this programatically:
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.contentSize.height);
I used storyboard with autolayout enable. I put a UIScrollView in storyboard. Then drag a UITableView over the scrollview. I set the table view's width bigger than the scroll view's width. I then set the constraints with the trailing space to scrollview with a negative number and storyboard fill all other constraints without error. The structure look like:
UIView
UIScrollView
UITableView
in code, I set the scroll view content size:
self.scrollView.contentSize = CGSizeMake(500, 400);
the size matches table view's size. But it doesn't work. The table view will not scroll horizontally. What I am doing wrong and how to make a UITableView scroll horizontally. I mean the whole table, not the each cell scroll horizontally. Thanks for your help.