I have a ScrollView with pictures. Since the transition to Xcode8 / Swift 3, the image no longer fits the size of ScrollView. Inside the image, keeps its original size when in Xcode 7/2 swift image which was perfectly adapted to scroll and matched perfectly. Below is the corresponding code:
import UIKit
class GoaLawahViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var menuButton: UIBarButtonItem!
#IBOutlet weak var ScrollView: UIScrollView!
#IBOutlet weak var PageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
let ScrollViewWidth:CGFloat = self.ScrollView.frame.width
let scrollViewHeight:CGFloat = self.ScrollView.frame.height
let imgOne = UIImageView(frame: CGRect(x: 0, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgOne.image = UIImage(named: "GoaLawah04.jpg")
let imgTwo = UIImageView(frame: CGRect(x: ScrollViewWidth, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgTwo.image = UIImage(named: "GoaLawah07.jpg")
let imgThree = UIImageView(frame: CGRect(x: ScrollViewWidth*2, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgThree.image = UIImage(named: "GoaLawah09.jpg")
self.ScrollView.addSubview(imgOne)
self.ScrollView.addSubview(imgTwo)
self.ScrollView.addSubview(imgThree)
self.ScrollView.contentSize = CGSize(width: self.ScrollView.frame.width * 3, height: 1.0)
self.ScrollView.delegate = self
self.PageControl.currentPage = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
// Test the offset and calculate the current page after scrolling ends
let pageWidth:CGFloat = scrollView.frame.width
let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1
// Change the indicator
self.PageControl.currentPage = Int(currentPage);
}
}
try to add your imageViews on a containerView(just a new blank UIView),then add the containerView on your scrollView
Attached is my new code and it works. against that by not understand is that I was forced to remove my position constraints for it to function properly
import UIKit
class PandangBaiViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var PageControl: UIPageControl!
#IBOutlet weak var ScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let ScrollViewWidth:CGFloat = self.ScrollView.frame.width
let scrollViewHeight:CGFloat = self.ScrollView.frame.height
let imgOne = UIImageView(frame: CGRect(x: 0, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgOne.image = UIImage(named: "1PadangBay.jpg")
imgOne.contentMode = UIViewContentMode.scaleAspectFit
self.ScrollView.addSubview(imgOne)
let imgTwo = UIImageView(frame: CGRect(x: ScrollViewWidth, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgTwo.image = UIImage(named: "2PadangBay.jpg")
imgTwo.contentMode = UIViewContentMode.scaleAspectFit
self.ScrollView.addSubview(imgTwo)
let imgThree = UIImageView(frame: CGRect(x: ScrollViewWidth*2, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgThree.image = UIImage(named: "PadangBaiinternet1.jpg")
imgThree.contentMode = UIViewContentMode.scaleAspectFit
self.ScrollView.addSubview(imgThree)
let imgFour = UIImageView(frame: CGRect(x: ScrollViewWidth*3, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgFour.image = UIImage(named: "PadangBaiinternet2.jpg")
imgFour.contentMode = UIViewContentMode.scaleAspectFit
self.ScrollView.addSubview(imgFour)
let imgFive = UIImageView(frame: CGRect(x: ScrollViewWidth*4, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgFive.image = UIImage(named: "PadangBaiinternet3.jpg")
imgFive.contentMode = UIViewContentMode.scaleAspectFit
self.ScrollView.addSubview(imgFive)
self.ScrollView.contentSize = CGSize(width: ScrollViewWidth * 5, height: scrollViewHeight)
self.ScrollView.isPagingEnabled = true
self.PageControl.currentPage = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
// Test the offset and calculate the current page after scrolling ends
let pageWidth:CGFloat = scrollView.frame.width
let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1
// Change the indicator
self.PageControl.currentPage = Int(currentPage);
}
}
Related
I'm learning to use UIScrollView to make several pictures could scroll horizontally. These images are programmatically added as a subview to a UIView within UIScrollView. The 'Scrolling enabled' is also set true. However, when I run the app, the UIScrollView cannot scroll.
Here is my code.
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var scrollContentView: UIView!
var imageViews = [UIImageView]()
override func viewDidLoad() {
super.viewDidLoad()
let imageWidth: CGFloat = 200.0 / 768.0 * 1024.0
for i in 1...3 {
let image = UIImage(named: "image\(i)")
let imageView = UIImageView(image: image)
imageViews.append(imageView)
let x: CGFloat = CGFloat(i - 1) * (imageWidth + 10)
scrollContentView.addSubview(imageView)
print(imageView.frame)
imageView.frame = CGRect(x: x, y: 0, width: imageWidth, height: 200.0)
}
scrollContentView.frame = CGRect(x: 0.0, y: 0.0, width: imageWidth * 3 + 20.0, height: 200.0)
scrollView.contentSize = scrollContentView.frame.size
}
}
Instead of creating the scrollContentView in Interface Builder, create it programmatically.
Xcode won't complain anymore about missing contraints, and you will be able to play with the frame manually.
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
var scrollContentView: UIView!
var imageViews = [UIImageView]()
override func viewDidLoad() {
super.viewDidLoad()
scrollContentView = UIView()
scrollView.addSubview(scrollContentView)
let imageWidth: CGFloat = 200.0 / 768.0 * 1024.0
for i in 1...3 {
let image = UIImage(named: "image\(i)")
let imageView = UIImageView(image: image)
imageViews.append(imageView)
let x: CGFloat = CGFloat(i - 1) * (imageWidth + 10)
scrollContentView.addSubview(imageView)
print(imageView.frame)
imageView.frame = CGRect(x: x, y: 0, width: imageWidth, height: 200.0)
}
scrollContentView.frame = CGRect(x: 0.0, y: 0.0, width: imageWidth * 3 + 20.0, height: 200.0)
scrollView.contentSize = scrollContentView.frame.size
}
}
I am unable to get UITextField to accept an input in iPhone 7 simulator. The code works fine in iPhone XR simulator. What is missing?
Full code below:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var containerView = UIView()
var textFieldFirstNameSignUp: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let screenSize = UIScreen.main.bounds.size
let width = screenSize.width - 60
let titleLabel = UILabel(frame: CGRect(x: 30, y: 80, width: width, height: 26))
titleLabel.font = UIFont.systemFont(ofSize: 32)
titleLabel.text = "Create New Account"
self.containerView.addSubview(titleLabel)
self.scrollView = UIScrollView()
self.scrollView.delegate = self
scrollView.contentSize = CGSize(width:1000, height:1000)
containerView = UIView()
scrollView.addSubview(containerView)
view.addSubview(scrollView)
containerView.addSubview(titleLabel)
textFieldFirstNameSignUp = UITextField(frame: CGRect(x: 30, y: 140, width: width, height: 50))
textFieldFirstNameSignUp.placeholder = "First Name"
textFieldFirstNameSignUp.font = UIFont.systemFont(ofSize: 18)
self.containerView.addSubview(textFieldFirstNameSignUp)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.frame = view.bounds
containerView.frame = CGRect(x:0, y:0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)
}
}
If I pinch the simulator, it does not work! I want to zoom in and out )=
Here is my Code:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
let onView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
scrollView.contentSize.width = 10000
scrollView.contentSize.height = 10000
onView.frame = CGRect(x: 0, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
scrollView.addSubview(onView)
for _ in 1...1000{
let x = arc4random_uniform(10000)
let y = arc4random_uniform(10000)
let btn = UIButton(frame: CGRect(x: Int(x), y: Int(y), width: Int(150), height: Int(100)))
btn.backgroundColor = .red
self.board.addSubview(btn)
}
scrollView.minimumZoomScale = 0.5
scrollView.maximumZoomScale = 10.0
scrollView.zoomScale = 1.0
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return scrollView
}
}
I have an UIScrollView and an UILabel on a ViewController. If the user arrives at the last page of the scrollView, the Label should be hidden. How can I do that? Here is my unfinished code:
swipeLabel.center.x = self.view.frame.width + 30
self.comingSoonLabel.isHidden = false
self.scrollView.frame = CGRect(x: 0, y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)
let scrollviewHeight = self.scrollView.frame.height
let scrollviewWidth = self.scrollView.frame.width
let imgOne = UIImageView(frame: CGRect(x: 0, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let imgTwo = UIImageView(frame: CGRect(x: scrollviewWidth, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let feedbackView = UIView(frame: CGRect(x: scrollviewWidth*4, y: 0, width: scrollviewWidth, height: scrollviewHeight))
imgOne.image = UIImage(named: "preview1")
imgTwo.image = UIImage(named: "preview2")
self.scrollView.addSubview(imgOne)
self.scrollView.addSubview(imgTwo)
self.scrollView.addSubview(feedbackView)
self.addChildFeedBack(feedbackView: feedbackView)
self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width * 3, height: self.scrollView.frame.height)
self.scrollView.isPagingEnabled = true
}
func addChildFeedBack(feedbackView: UIView){
let contestStoryboard = UIStoryboard(name: "feedback", bundle: nil)
let testVC = contestStoryboard.instantiateViewController(withIdentifier: "FeedbackViewController") as! FeedbackViewController
testVC.view.frame = feedbackView.bounds
feedbackView.addSubview(testVC.view)
self.addChildViewController(testVC)
testVC.didMove(toParentViewController: self)
self.comingSoonLabel.isHidden = true
}
You can get pageNumber using scrollview's delegate method.
Note -You need to assign delegate of scrollview and conform that delegate also.
scrollview.delegate = self //assign
class testClass: UIViewController, UIScrollViewDelegate // conform
Delegate
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
if pageNumber == 3{
// hide your label here
}
// do you logic related to last page
}
I am trying to align my images vertical in my scrollView. Unfortunately I can't figure out what the problem is. I think it's something mathematical and this is not my strongest thing. :(
I hope someone can help me out with this problem. I will also provide my code and image of the problem below:
//
// muscleListVC.swift
// ActiveRest
//
// Created by Fhict on 04/10/16.
// Copyright © 2016 Kevin Vugts. All rights reserved.
//
import UIKit
class muscleListVC: UIViewController {
var images = [UIImageView]()
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
print("Count: \(images.count)")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
var contentHeight: CGFloat = 0.0
let scrollHeight = scrollView.frame.size.height
for x in 0...4 {
let image = UIImage(named: "muscle\(x).png")
let imageView = UIImageView(image: image)
images.append(imageView)
var newX: CGFloat = 0.0
newX = scrollHeight / 4 + scrollHeight * CGFloat(x) / 4
print("the size is \(newX)")
contentHeight += newX
scrollView.addSubview(imageView)
imageView.frame = CGRect(x: 0, y: newX, width: view.frame.size.width, height: 166)
print("the content height: \(contentHeight)")
scrollView.contentSize = CGSize(width: scrollView.frame.size.width, height: contentHeight)
}
scrollView.clipsToBounds = false
}
}
Thank you very much! <3
I am not quite sure what you intend to do but I tried to modify your code a bit to make it work.
override func viewDidLoad() {
super.viewDidLoad()
let numberOfImages = 5
let imageViewHeight:CGFloat = 166.0
scrollView.contentSize = CGSize(width: view.frame.size.width, height: imageViewHeight*(CGFloat)(numberOfImages))
var newY:CGFloat = 0
for x in 0...(numberOfImages-1) {
let image = UIImage(named: "muscle\(x).png")
let imageView = UIImageView(image: image)
images.append(imageView)
imageView.frame = CGRect(x: 0, y: newY, width: view.frame.size.width, height: imageViewHeight)
newY = newY + CGFloat(imageViewHeight)
scrollView.addSubview(imageView)
}
}