Swift Scroll View not Scrolling - ios

import UIKit
class ContactViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(scrollView)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
scrollView.contentSize = CGSize(width: 375, height: 800)
}
// Do any additional setup after loading the view.
}
For the past 2 days I have been trying to get my scroll view to work and its not working. I am very very new to swift and have no idea what to do. The app itself will run however it will simply not scroll up and down !!!! help please i am desperate !!!!

Related

Unable to override scrollViewDidScroll from UIScrollViewDelegate

I am new to iOS and swift, I was trying to override scrollViewDidScroll() from UIScrollViewDelegate but it was showing me "Method does not override any method from its superclass" , I was following this tutorial https://www.youtube.com/watch?v=XQ3CVd8-zNE
This is the code I did.
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrolliew: UIScrollView!
#IBOutlet weak var pageControl: UIPageControl!
var contentWidth:CGFloat=0.0
override func viewDidLoad() {
super.viewDidLoad()
scrolliew.delegate=self
for image in 0...3 {
let imageToDisplay = UIImage(named:"\(image).png")
let imageView = UIImageView(image:imageToDisplay)
scrolliew.addSubview(imageView)
let xCordinates = view.frame.minX + view.frame.width * CGFloat(image)
contentWidth += view.frame.width
imageView.frame=CGRect(x:xCordinates,y:view.frame.height/2,width:100,height:100)
}
scrolliew.contentSize=CGSize(width: contentWidth, height: view.frame.height)
}
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
//my code here
}
}
You don't actually have to override the method. scrollViewDidScroll(...) is part of the UIScrollViewDelegate protocol whose implementation is optional.
Just remove the override keyword in order to fix the error.

Scroll View User Interaction Disabled Swift 4

I have a swift project, and I created the following layout structure:
And it looks like this:
Everything's fine, BUT: I don't know why the scroll view is not working. And I can't do user interaction besides the play button at the bottom.
Here is my swift file:
#IBOutlet weak var playButton: UIButton!
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.isScrollEnabled = true
scrollView.isUserInteractionEnabled = true
scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height+300)
}
What should I do?
I have the same problem on another view, where I have a WebView as the main part of the screen.
Thank you.

I want to scroll every thing except button in view

I am using scroll view which scrolling everything perfectly but i have button at the top of view for dissmissing view i want it to remain there and do not scroll in swift .
Add first UIScrollView and then UIButton to self.view.
Add button outside scrollview content like overlay.
Try this solution:
class TmpVC: UIViewController, UIScrollViewDelegate {
#IBOutlet var scoll : UIScrollView!
#IBOutlet var btn : UIButton!
var btnPoint : CGPoint!
override func viewDidLoad() {
super.viewDidLoad()
scoll.contentSize = CGSize(width: 320, height: 600)
scoll.delegate = self
btnPoint = btn.frame.origin
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
btn.frame.origin = CGPoint(x: btnPoint.x, y: btnPoint.y + scrollView.contentOffset.y)
}
deinit {
print("deinit Tmpvc")
}
}

Swift 3 UIScrollview not asking zooming

This is driving me nuts!
I'm basing my UIScrollView on http://koreyhinton.com/blog/uiscrollview-crud.html to make it programatic, so have set up a container view inside my scrollview. But it pans, but won't zoom.
class BinaryTreeViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var containerView : UIView!
override func viewDidLoad() {
super.viewDidLoad()
let width:CGFloat = self.view.bounds.width
let height:CGFloat = self.view.bounds.height
scrollView = UIScrollView()
scrollView.delegate = self
scrollView.minimumZoomScale = 0.5
scrollView.maximumZoomScale = 2.0
scrollView.contentSize = CGSize(width: width*2, height: 2000)
scrollView.backgroundColor = .red
containerView = UIView()
scrollView.addSubview(containerView)
view.addSubview(scrollView)
containerView.isUserInteractionEnabled = true
scrollView.isUserInteractionEnabled = true
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.frame = view.bounds
containerView.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)
}
override func viewWillLayoutSubviews() {
//I create a view called "theView"
containerView.addSubview(theView)
}
The following functions do not fire at any point
func update(zoomScale: CGFloat, offSet: CGPoint) {
scrollView.zoomScale = zoomScale
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return containerView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
NSLog("scroll")
}
You really don't need to do so much code for that purpose.
You can set up all you need for scrollView in storyboard, and you only need outlet for the view you wish to zoom.
Set up a controller, add scrollview, connect delegate property to view controller, add zooming view as subview in IB.
In the class, conform controller to UIScrollViewDelegate, and use viewForZooming, a scrollView delegate method.
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var zoomer: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return zoomer
}
}
P.S. Use newer resources for learning, Ray Wenderlich, AppCoda, etc - its a big web full of good sources, and Swift is in constant change.

setContentOffset to start at the bottom of scrollview not working

Hi guys I’m a total newbie in Xcode, i have this view controller that has a scrollView of height 2000. I want that when it loads, it starts at the bottom of the scrollView.
I have read that setContentOffset should help me with this but i just can’t make it work. I don’t know what I’m doing work. Any help will be really appreciated.
Thanks,
import UIKit
class Pantalla2: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let off = CGPointMake(0, 2000)
self.scrollView.setContentOffset(off, animated: true)
}
Try this
!
override func viewDidLoad() {
self.scrollview.contentOffset = CGPoint(x: 0, y: 2000)
}

Resources