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)
}
Related
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 add UIViewController into UIScrollView.
I searching and tried, but I can't find solution.
The most understandable way is this.
Here is my code.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.contentSize.width = self.view.frame.width * 2
let test = self.storyboard?.instantiateViewController(withIdentifier: "Test") as! TestViewController
self.addChild(test)
self.scrollView.addSubview(test.view)
test.willMove(toParent: self)
test.view.frame.origin = CGPoint.zero
}
}
But, I this way is error like this.
How to add UIViewController into UIScrollView??
Please Help me.
In your storyboard, just drag and drop a containerView, it will make a UIViewController
in your scrollView as you want
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")
}
}
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 !!!!
I am looking for the text to be scroll all the way down to the bottom that is it. Just like you would see in movie credits. I do not want to move the x, y location of the textview just the text inside from top to bottom.
import UIKit
class ViewController: UIViewController {
#IBOutlet var text: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func play(_ sender: Any) {
}
}
Use a UIScrollView and scroll a UILabel with the text in it.
You can use standard UIView animation with timing and optionally animation curve and set the contentOffset inside the closure.
UIView.animate(withDuration: 10.0) { // 10 seconds
scrollView.contentOffset = CGPoint(x: 0, y: maxDesiredScrollPosition)
}