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.
Related
I'm a newbie to swift development and I was looking for help for this use case of panning, zooming image and gestures.
On android this library provides all the functionalities
https://github.com/davemorrissey/subsampling-scale-image-view
Any help from people who have solved this on the iOS ?
If you're allowed/able to use UIKit, this works very well for me:
import UIKit
class PanZoomImageViewController : UIViewController, UIScrollViewDelegate
{
#IBOutlet private weak var imageView: UIImageView!
#IBOutlet private weak var scrollView: UIScrollView!
var image: UIImage?
override func viewDidLoad()
{
super.viewDidLoad()
scrollView.contentSize = CGSize(width: image?.size.width ?? 0, height: image?.size.height ?? 0)
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 4.0
imageView.image = image
}
#IBAction private func dismiss()
{
dismiss(animated: true)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
return imageView
}
}
I'm doing official iOS development tutorial from Apple. There is a task to make a zoomable image in a scrollView.
I believe that did it right. There is mistake, since I cant zoom image in.
The task is below in screenshots.
Task part 1
Task part 2
Here is my code:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//set the scroll view's deligate to be the viewcontroller instance
self.scrollView.delegate = self
viewForZooming(in: scrollView)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
override func viewDidAppear(_ animated: Bool) {
updateZoomFor(size: view.bounds.size)
}
func updateZoomFor(size: CGSize) {
let widthScale = size.width / imageView.bounds.width
let heightScale = size.height / imageView.bounds.height
let scale = min(widthScale, heightScale)
scrollView.minimumZoomScale = scale
scrollView.zoomScale = scale
}
}
the thread 1 error for which it gives me a fatal error, the code works but when I get it it does not let me do it gives that error and I don't know how to solve it puestwo that I have no value in the scrollView.delegate = self
import UIKit
class GaleriaBodas: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var pageControl: UIPageControl!
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let anchoPagina : CGFloat = scrollView.frame.width
let paginaActual : CGFloat = floor(scrollView.contentOffset.x - anchoPagina/2) / anchoPagina + 1
self.pageControl.currentPage = Int(paginaActual)
}
}
Connect scrollView via Storyboard with your scrollView variable in Code.
I use iCarousel module into my CollectionView. The Carousel View is in my UICollectionViewCell.
My code is as following.
class CelebrityDetailHeaderCell: UICollectionViewCell, UITextFieldDelegate, SDCycleScrollViewDelegate, iCarouselDataSource, iCarouselDelegate {
#IBOutlet weak var backgroudLab: UILabel!
#IBOutlet weak var name_en: UILabel!
#IBOutlet weak var name_tw: UILabel!
#IBOutlet weak var intro: UILabel!
#IBOutlet weak var DetailImg: UIImageView!
#IBOutlet weak var iCarouselView: iCarousel!
let screenWidth = UIScreen.mainScreen().bounds.width
let screenHeight = UIScreen.mainScreen().bounds.height
override func awakeFromNib() {
super.awakeFromNib()
iCarouselView.delegate = self
iCarouselView.dataSource = self
iCarouselView.type = .Linear
}
func numberOfItemsInCarousel(carousel: iCarousel) -> Int {
return 1
}
func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
let tempView = UIView(frame: CGRect(x: 0, y: screenWidth/2, width: screenWidth, height: screenWidth/2))
tempView.backgroundColor = UIColor.redColor()
return tempView
}
func carousel(carousel: iCarousel, valueForOption option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
if option == iCarouselOption.Spacing {
return 1.2
}
return value
}
}
The error message is as following
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSObject numberOfItemsInCarousel:]: unrecognized selector sent to instance 0x7f99bb3734a0'
I have set numberOfItemsInCarousel why the error message showed this. Does anyone help me? Thanks.
Look you need to add a dealloc method in your cell. As your cell is getting deallocated and the iCarousel delegate is calling that method to some deallocated instance thats why it is crashing
write a dealloc method like this
- (void)dealloc{
iCarouselView.delegate = nil
iCarouselView.dataSource = nil
}
I'm making an application where I need an x amount of custom UIView and place them in a scrollView. The layout of the scrollView and the UIView xib are set with AutoLayout. The result I'm getting now is this:
First View is well centred in ScrollView
Second View is wrong and has a lot of space in between
See my VC Code under here.
let sponsors = Sponsors.createSponsors() <-- Array
override func viewDidLoad() {
super.viewDidLoad()
configureSponsors()
}
//MARK: Load the AddView in ScrollView
func configureSponsors() {
self.scrollView.contentSize = CGSizeMake(CGFloat(sponsors.count) * self.scrollView.frame.size.width, self.scrollView.frame.size.height)
for sponsor in sponsors {
numberOfItems++
let addView = NSBundle.mainBundle().loadNibNamed("AddView", owner: self, options: nil).last as! AddView
addView.addDataToAddView(sponsor)
addView.frame = CGRectMake(CGFloat(numberOfItems - 1) * scrollView.frame.size.width, 0, scrollView.frame.size.width, scrollView.frame.size.height)
self.scrollView.addSubview(addView)
}
}
And here is my UIView code:
//MARK: Outlets
#IBOutlet weak var backgroundImageView: UIImageView!
#IBOutlet weak var sponsorTitle: UILabel!
#IBOutlet weak var sponsorLogo: UIImageView!
#IBOutlet weak var sponsorSubtitle: UILabel!
#IBOutlet weak var roundView: UIView!
#IBOutlet weak var playMusicButton: UIButton!
//MARK: Properties
var cornerRadius: CGFloat = 3.0
func addDataToAddView(sponsor: Sponsors) {
backgroundImageView.image = UIImage(named: sponsor.backgroundImage)
sponsorLogo.image = UIImage(named: sponsor.logoImage)
sponsorTitle.text = sponsor.title
sponsorSubtitle.text = sponsor.subTitle
}
override func layoutSubviews() {
roundView.layer.cornerRadius = roundView.frame.size.width / 2
roundView.alpha = 0.7
roundView.clipsToBounds = true
}
//MARK: PlayVideo
#IBAction func playVideo(sender: UIButton) {
//play music
}
I've already searched for the same problems. I found out that I have to use the Pure Auto Layout Approach. Should that mean I need to programatically set the constraints for the UIView?
Many thanks,
Dax
Update:
Pictures for scrollView setup:
You should not perform layout calculations in viewDidLoad as frames are not set correctly till then.
Correct place to do this is either viewWillLayoutSubviews or viewDidLayoutSubviews as you will get the correct frame data when these functions are called