Pressing_Keys_24_7
Pressing_Keys_24_7

Reputation: 1863

How to add a Label for Each Image in the iCarousel View Framework

I have used the iCarousel Framework in my View Controller. But, I am not sure how can I add a label below each image while scrolling in the carousel view. Would appreciate your help! Thanks!

 func numberOfItems(in carousel: iCarousel) -> Int {

return 10

}

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {

let view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width/1.4, height: 300))
view.backgroundColor = .red

let imageview = UIImageView(frame: view.bounds)
view.addSubview(imageview)
imageview.contentMode = .scaleToFill
imageview.image = UIImage(named: "Dog_\(index+1)")

return view

}

func carouselDidEndScrollingAnimation() {
  
if (myCarousel.currentItemIndex == 1) {
    
    dogNameLabel.text = "Brownie!"
    
}

}

let myCarousel: iCarousel = {
let view = iCarousel()
view.type = .rotary
return view
}()

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(myCarousel)
myCarousel.dataSource = self
myCarousel.frame = CGRect(x: 0, y: 220, width: view.frame.size.width, height: 400)
}

Upvotes: 0

Views: 119

Answers (1)

Volkan Sonmez
Volkan Sonmez

Reputation: 785

There are too many option. You can create custom view (xib) for reusability. You need to label in contentView above imageView.

But you need to create view like that.

  func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
        
        let view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width/1.4, height: 300))
        view.backgroundColor = .red
        
        let imageview = UIImageView(frame: view.bounds)
        view.addSubview(imageview)
        imageview.contentMode = .scaleToFill
        imageview.image = UIImage(named: "Dog_\(index+1)")
    
        let label = UILabel()
        label.text = "Enter your text"
        label.frame = CGRect(x: 0, y: 20, width: view.frame.size.width, height: 30)
        //or use constraints
    
        view.addSubview(label)
        
        return view
        
   }

Upvotes: 1

Related Questions