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)
Related
so I have added scrollview to my screen.
However, I'm unable to scroll up and down.
My scrollview width and size are 414 and 784 respectively while my content size is scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 800)
Why can't I scroll up and down?
How do I fix this?
You can add a UIView (call it content view) inside your scrollView with top, botttom, left, right constraints to zero. And give height constraint = 800 for this view.
This should work!
Basically, you need to add some content to the scrollView. If the height of contents becomes greater than the scrollView height, the scrollView becomes scrollable.
I am working with Swift 4.0 where I want UITableview with both vertical and horizontal scroll.
I have set contentSize of my UITableview. If I change the frame of UITableview programmatically then contentSize of UITableview is not working.
If the frame is same as it is set using autolayout then the contentSize of UITableview is working fine.
Below is the code I have tried :
let height : CGFloat = CGFloat((self.transactionArr.count*60)+70)
let width = self.view.frame.size.width*2
self.tblWidth.constant = self.view.frame.size.width
self.view.updateConstraintsIfNeeded()
self.tableView.contentSize = CGSize(width: width, height: height)
Don't hack UITableView - it is designed to scroll only vertically. Check out UICollectionView instead, it is designed for both vertical and horizontal scrolling (although you can make it scroll just one way if that's your need) - docs, or a tutorial.
You can put your tableview in a ScrollView. Make sure your ScrollView size is the same with tableview.
set your ScrollView width bigger than your container view and you can scroll your tableview horizontally.
This is my setup:
I do not know what I am doing wrong. The image view is bigger than the size of the view and of the scroll view. The constrains are set al followed:
Scroll view: equal heights to View * 0,5, equal width to View, center Y and X to View.
View (inside Scroll view): pinned all zero's inside Scroll view, equal heights and width. I also tried instead of equal heights and widths to center X and Y inside Scroll view, but it won't scroll.
How can I let the Scroll view scroll? Thank you.
Add a leading, trailing and top constraint and equal height of UIScrollView to superview with 0.5 multiplier. Now to your contentView (the UIScrollView subview), add a leading, trailing , top and bottom constraint. Also add equal height and width to UIScrollView. Set the height to a priority of 250. Add constraints for UIImageView inside this contentView.
Since the contentView will have a fixed height of low priority equal to the UIScrollView height. This fixed height constraint will break once the UIImageView total height(based on the constraints you add) will get larger than the UIScrollView height and the content will become scrollable. So at the very least you will always have a view half the screen size and become scrollable once the content becomes too large vertically.
You need to give contentSize to scrollview.
ScrollView.contentSize = CGSize(width: 1000, height: 500)
Which constrains have you given to imageview?
set constraint of imageview:
Trailing ,leading,top,bottom - 0 and also give height constraint.
I have a situation where i have following view layout.
-UIViewcontroller
-SCrollView
-UIView(outer)
-buttons
-labels
-UIView(inner)
-labels
-buttons
-buttons
the inner UIView height can be any longer as content inside is dynamically added.
So the main issue is i can not scroll till the end of the content, instead i can only scroll till the height of inner UIView.
NOTE - I am using auto layout
Any help is much appreciated.
Thanks
You should change the frame of your inner view dynamically based on the content you are adding. According to your inner view hierarchy, you have label and button. You might be setting some dynamic text on label and also doing some manipulation with button . Use the following method to get the height of the text ,
- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height);
}
return size;
}
Here font is the font you set to button and label. Call this method by passing the text added on label and button. CGSize has a height property , add height value of label and button. Now set this sum height to innerView by calling innerView.frame.size.height = <height>. This increase the height of your innerView based on content inside that.
Put all your buttons in the bottom inside a container view and add the following constraints to the outerView (assuming that the new container you've added is called "buttonContainer"):
A vertical spacing constraint between the bottom of innerView and the top of the buttonContainer.
A vertical spacing constraint between the bottom of the buttonContainer and the bottom of the outerView.
Horizontal spacing constraints between between the buttonContainer and outerView to force it to full width.
When using AutoLayout, you have to have enough constraints to properly define the contentSize of the scrollView.
For more information about using AutoLayout with UIScrollView, check out the following links by Apple:
Working with Scroll Views
UIScrollView and Autolayout
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.