UIScrollView not scrolling in Swift - ios

My UIScrollView won't scroll down. I don't know why. I already followed Apple documentation regarding to this issue.
#IBOutlet weak var scroller: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
scroller.scrollEnabled = true
// Do any additional setup after loading the view
scroller.contentSize = CGSizeMake(400, 2300)
}

You need to set the frame of your UIScrollView so that it is less than the contentSize. Otherwise, it won't scroll.
Also, I would recommend that you add the following to your viewDidLoad method:
scroller.contentSize = CGSize(width: 400, height: 2300)

If you are using AutoLayout
Set content size in viewDidAppear which works for me.
override func viewDidAppear(_ animated: Bool) {
scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height+300)
}

Alot of the time the code is correct if you have followed a tutorial but what many beginners do not know is that the scrollView is NOT going to scroll normally through the simulator. It is suppose to scroll only when you press down on the mousepad and simultaneously scroll. Many Experienced XCode/Swift/Obj-C users are so use to doing this and so they do not know how it could possibly be overlooked by beginners. Ciao :-)
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
// Do any additional setup after the view
}
override func viewWillLayoutSubviews(){
super.viewWillLayoutSubviews()
scrollView.contentSize = CGSize(width: 375, height: 800)
}
This code will work perfectly fine as long as you do what I said up above

Do not give fix height to scroll view and always give top of first subview to scrollview and bottom of last subview to scrollview. By this way scroll view will automatically grow as per the size of contained subviews. No need to give contentSize to the scrollview.It will work for small as well as large size iPhone.

Swift 3.0 version
scroller.contentSize = CGSize(width: scroller.contentSize.width, height: 2000)

If you are using autolayout, then the contentSize property stops working and it will try to infer the content size from the constraints. If that is the case, then your problem could be that you are not defining the necessary constraints to the content view so that the scrollview can infer the content size.
You should define the constraints of your content view to the top and bottom edges of the scrollview.

If you are using Storyboard:
Put your Content view inside the UIScrollView
Add top, bottom, left and right constraints with the scroll view
Add equal heights and widths constraints
For a vertical scroll set the Equal Heights Constraint priority to 250. For a horizontal scroll set the Equal Widths Constraint priority to 250

In my case, I used UIStackView inside UIScrollView.
Added some views-elements from code to stackview.
It won't scroll.
Fixed it by setting stackview's userInteractionEnabled to false.

The problem could be that your scrollView doesn't know its contentSize like stated above, but the fix is easier than what the above answers are. Like Carlos said but I will elaborate more. If you want your scrollView to scroll vertically(up & down), make your contentView which is in the hierarchy of the scrollView equal width to the ViewController and give it a height constraint that works for your project i.e. 700. For the opposite(horizontally) make the height equal to the ViewController and the width some big number that works for your project.

FWIW, I found that I needed to use sathish's solution from above, though it was insufficient to effect the intervention in viewDidAppear alone. I had to instead make the adjustment for every new content assignment:
func display(pattern: Pattern) {
let text : NSAttributedString = pattern.body()
patternTextView.attributedText = text
// Set the size of the view. Autolayout seems to get in the way of
// a correct size calculation
patternTextView.contentSize = CGSize(width: 348, height: 620)
}
The manifest constants (yeah, I know, yuk, but it makes it easier to understand here) are from the autolayout spec.

It worked for me. In Size Inspector
Layout = Translates Mask into constraints.
Autoresizing = all click.

For Swift 5.6 and iOS 15:
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
let subView: UIView = UILabel()
subView.text = String(repeating: "MMMMMMM ", count: 100)
NSLayoutConstraint.activate([
subView.topAnchor.constraint(equalTo: scrollView.topAnchor),
subView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
subView.leftAnchor.constraint(equalTo: scrollView.leftAnchor),
subView.rightAnchor.constraint(equalTo: scrollView.rightAnchor),
// Constrain width so the label text wraps and we scroll vertically.
subView.widthAnchor.constraint(lessThanOrEqualTo: scrollView.widthAnchor),
])

Increase the content Height work for me.

I do not know it is a good solution, but you can try to set headerview to empty UITableView.
let scrollView: UIView = UIView(frame: CGRectMake(0, 0, 400, 2300))
tableView.tableHeaderView = scrollView

Related

UITableViewController layout not working

I am trying to position UITableView to the left side of the app, whole height but taking just 1 / 3 of the available width with the code like this:
class ViewController: UIViewController {
var tableController: UITableViewController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableController = UITableViewController();
addChildViewController(tableController!)
self.view.addSubview(tableController!.view)
tableController!.didMove(toParentViewController: self)
}
override func viewDidLayoutSubviews() {
tableController!.view.backgroundColor = UIColor.red
tableController!.view.frame = CGRect(
x: 0,
y: 0,
width: view.bounds.width / 3,
height: view.bounds.height);
//tableController!.view.frame = view.bounds
}
}
And it looks like this:
And I don't get why the lines are not correctly aligned horizontally and it looks like being cut on the right.
If I give the view controller full width / height by uncommenting the last line, it looks better:
Question 1
Are you properly constraining the TableView in the parent view? You can set proportional constraints. Please see this post on the topic: AutoLayout to keep view sizes proportional
Side Note
For iOS 11, constraining views to layout guides will trigger warnings in your IDE. Consider embedding UI components inside another view that takes up the entire width of the parent view using Auto-resizing Masks. See the topic here: Xcode Auto Layout Resizing Issue

UICollectionView width programatically conflicting with storyboard initialization

I've created a UICollectionView through the storyboard and temporarily positioned it in the view; however, I'd like to overwrite the positions programmatically on launch rather than using the storyboard configuration.
I thought viewDidLoad() was the right location to place make this change?
Swift 2 Code:
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
collectionView.backgroundColor = UIColor(white:1, alpha:0.0)
// Doesn't stretch the width to the device's width on launch. :-(
collectionView.frame.size = CGSize(width: UIScreen.mainScreen().bounds.width, height: 200)
}
If you are setting constraints and then want to change height or width then you should take outlet of that height or width and increament it's constant value. just ctrl+drag from width constraint to class and make outlate and then increment that outlate's contant property in viewdidload.
for example,
#IBOutlet weak var heightConstraint: NSLayoutConstraint!
self.heightConstraint.constant = 200
And second thing if you want to keep your collectionview's height equal with device then there is no need to set programatically. autolayout manage it if you set proper constraint. so main point is use autolayout at evrywhere or not at all. dont mix both autolayout and then set frame programatically.
hope this will help :)

UIScrollView Cutting Off Bottom Content

I'm having an extremely sad time trying to get my scrollView to display content correctly.
For one page it seems to show a heap of blank content at the bottom and for another it cuts off before reaching the bottom... extremely sad. I've set the constraints of the scrollView to 0 for each side.
Xcode screenshot
Screenshot of all the space
I've been playing around with trying to set the contentSize but that doesn't seem to do anything.
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height)
print("Width: \(self.view.bounds.size.width) Height: \(self.view.bounds.size.height) scrollView: \(self.scrollView.contentSize)")
// prints -> Width: 375.0 Height: 667.0 scrollView: (375.0, 667.0)
}
Hard to tell what it may be with only the screenshot, but I have some suggestions. Select the ScrollView, then the ruler and:
Check if the indicator insets are all zeros.
In arrange select fill container horizontally and fill container vertically.
Try to change the height value of the height to check if it is set bigger than it needs to be.
Below a screenshot of what you should see

UIScroll View Not Working As Expected

I have a scene where I just want the scroll to be vertical not horizontal. I have tried to program it to disable but that doesn't seem to work. I also run into the issue where it isn't auto resizing to different phone screens. Im kinda new at this and I'm just really stuck. Any help?
Disable Code:
#IBOutlet var scrollview: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollview.contentSize.height = 800
scrollview.contentSize.width = 0
I think problem is with this line
scrollview.contentSize.width=0
Try to set width equal to screen width like this,
scrollview.contentSize.width=[UIScreen mainScreen].bounds.size.width;
Scrollview will scroll horizontally when it's contentSize.width is greater than viewcontroller's width.
Hope it will solve the issue.
I think you are not applying constraints on UIScrollView properly now first of all you have to properly add constraint on scrollview and no need to set contentsize from code.
http://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/
Hope this tutorial will help you better understand the ScrollView
var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView = UIScrollView()
self.scrollView.contentSize = CGSizeMake(1000, 1000)
self.view.addSubview(scrollView)
}

Why can't I change the view's frame size in Swift?

So this is the weirdest thing I came across ever.
I add a simple UIView on top of an UIView on an UIViewController. I change the size to 200 x 200 and then I want to change the UIView's size by placing this line of code inside the viewDidLoad method:
self.gameBackgroundView.frame.size.height = 10
which seems perfectly normal in Swift. Though, nothing happens. I can change the color, corner radius etc...
EDIT:
I have also deleted all constraints from that view and it still does not work.
Ok, So I found a solution here on stack ..
Change frame programmatically with auto layout
The correct answer, that works is, that:
I need to create an IBOutlet of my heightConstraint
Change the value of that constraint accordingly
Thank you all!
It is because of Auto Layout, however, you could do it after Auto Layout is done its work or change constraints of it. Set it your frame in your viewDidAppear() instead. It should work.
I hope this helps you.
Try this code, it will work on any device
let BackImage = UIImage(named: "backGrndImg#2x.png")!
var imageView: UIImageView!
override func loadView() {
super.loadView()
self.imageView = UIImageView(frame: CGRectZero)
self.imageView.contentMode = .ScaleAspectFill
self.imageView.image = BackImage
self.view.insertSubview(imageView, atIndex: 0)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.imageView.frame = self.view.bounds
}

Resources