How to Set Scrollview Content Size in Swift 3.0

how to set scrollview content size in swift 3.0

From Swift 3 CGSizeMake is not available use CGSize init instead.

scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 700)

Check the Apple Documentation on Core Graphics as of from Swift 3 they have made a lot of changes.

Change UIScrollView Content size

there was an error in the this function:

override func viewDidLayoutSubviews() {
self.scrlMain.translatesAutoresizingMaskIntoConstraints = true
self.scrlMain.contentSize = CGSize(width: self.scrlMain.frame.width, height: 60.0 + 5 * (self.scrlMain.frame.width / 16.0 * 5.0));
}

I delete this code:

self.scrlMain.contentSize = CGSize(width: self.scrlMain.frame.width, height: 60.0 + 5 * (self.scrlMain.frame.width / 16.0 * 5.0));

And all was right

How do I auto size a UIScrollView to fit its content

The best method I've ever come across to update the content size of a UIScrollView based on its contained subviews:

Objective-C

CGRect contentRect = CGRectZero;

for (UIView *view in self.scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
self.scrollView.contentSize = contentRect.size;

Swift

let contentRect: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in
rect = rect.union(view.frame)
}
scrollView.contentSize = contentRect.size

Swift: How to set UIScrollView contentSize height to height of contents?

Does this mean I have to set the contentSize after I add all the subviews?

Basically yes. Your goal should be to make the scroll view small and the content size big. That is what makes a scroll view scrollable: its contentSize is bigger than its own bounds size. So, set the scroll view itself to have a frame that is the same as the bounds of its superview, but then set its contentSize to embrace all its subviews.

How to set size of UIScrollView?

CGSizeMake is deprecated in Swift 3. Use regularCGSize instead. Your code should look like this:

self.scrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.height)

The main only difference between them is that you have to type in the parameter names (width, and height) for CGSize, while you do not need to for CGSizeMake.

You can take a look at this question for additional information: What's the difference between using CGSizeMake and CGSize? Is one better than the other?

Simply put, CGSize, is used more commonly in Swift because it's considered more "Swifty". CGSizeMake is a leftover from Objective-C

How to edit UIScrollView size?

You are not giving the correct frame to the cells or tickets inside the scrollview.

var lastCellMaxX: CGFloat = 0
var constantSpacingBetweenCell: CGFloat = 10

for i in 0 ..< 3 {
slides[i].frame = CGRect(x: lastCellMaxX, y: 0, width: 188, height: 153)
slides[i].backgroundColor = UIColor.red
scrollView.addSubview(slides[i])
lastCellMaxX += 188 + constantSpacingBetweenCell
}

I didn't get what is view.frame and I think that is also of no use in your case.
Store the lastcellMaxX which is cell.origin.x + cellwidth + spacingBetweenCell,
That will be the x while giving frame to the next view

How to set UIScrollView Height in Swift

1) ScrollView Contraints

Sample Image

2) ScrollView -> contentView Constraints to scroll View same as above image

3) now ContentView width and Height Constraints to main View [SuperView in which ScrollView is embedded] and constraints will be as follows.

4) now click on the EqualWidth to View [Third constraint from top]and edit it as in step 6

Sample Image

5) contentView Width Contraint

Sample Image

6) ContentView Height Constraint // set priority [must] . here you need to change the first item and second item in the menu to as shown First as - ContentView.Height and second as - View.height and set priority to 250 after this a dotted line will appear in storyboard along the contentView

Sample Image

7) now add the content like [UIView, labels, textfields] in contentView and add constraints as Top upperMost view top space to contentView [like I have]DoubleRight imageView

Sample Image

and constraints for my DoubleRight imageView are

Sample Image

look for the Top space margin its given a top space 20 points

and same you need to do for the last item you will be adding in ContentView like I have uiView

Sample Image

add bottom space from this respective view to your superView[ContentView] and my constraints are:

Sample Image

after you had initialed all these steps results will be as Expected for every Screen size no need to change height additionally for screen sizes

Note : - [all the views must be connected to each other with top and bottom parameter]

like Flow will be

View1 - top to contentView and bottom to View2
View2 - top to View1 and bottom to view3
View3 [Last view] - top to View2 and bottom to contentView as shown

using uiView, uiimageViews their heights must be fixed

SizeToFit on a UIScrollView's content size

At the moment, this is the best I have:

CGRect contentRect = CGRectZero;
for (UIView *view in self.subviews)
contentRect = CGRectUnion(contentRect, view.frame);
self.contentSize = contentRect.size;


Related Topics



Leave a reply



Submit