Why are iCarousel items scrolling beyond boundaries?

100 views Asked by At

Hey all I am using iCarousel and all is going well except that thee items placed are scrolling beyond the boundaries given to the carousel How to make sure that the items surpassing those boundaries are hidden?

carousel variable:

lazy var car:iCarousel = {
        let view = iCarousel()
        view.frame = CGRect(x:0, y:Int(CGFloat(Double(Int(Float(bounds.size.height*0.05))) + globW  )) , width: Int(Float(bounds.size.width)*0.85), height: Int(Float(bounds.size.height)*0.75))
        view.type = .coverFlow
        view.isVertical = true
        view.bounces = true
        
//        view.backgroundColor = .red
        return view
    }() 

carousel functions:

 func numberOfItems(in carousel: iCarousel) -> Int {
       return currentField.count
    }

    func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
        let v = UIView(frame: CGRect(x: 0, y:0, width: car.bounds.width, height: car.bounds.height*0.5))
        
            if currentField[index].type == "A"{
                let cont=UITextField()
                let img=UIImageView()
                img.frame=CGRect(x: Double(Int(Float(bounds.size.width*0.05))), y: Double(v.bounds.height)*0.5, width:Double(bounds.size.width)*0.1, height: Double(bounds.size.width)*0.1)
           
                img.image=UIImage(named: currentField[index].path)?.resized(to: CGSize(width: CGFloat(bounds.size.width*0.1), height: CGFloat(bounds.size.width*0.1)))
              

                cont.textColor = .white
                cont.attributedPlaceholder = NSAttributedString(string: currentField[index].placeholder, attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray])
                cont.setBottomBorder(color:UIColor.gray.cgColor)
                cont.frame = CGRect(x: CGFloat(Int(Float(car.bounds.width*0.4))), y: v.bounds.height*0.5 /*CGFloat(Int(Float(car.frame.height*0.5)))-25*/, width: CGFloat(Int(Float(bounds.size.width)))-CGFloat(Int(Float(bounds.size.width*0.2))), height: 50)
            cont.backgroundColor = .clear
                v.addSubview(img)
           v.addSubview(cont)
                cont.addTarget(self, action: #selector(editTypeA(_:)), for: UIControl.Event.editingChanged)

            }
   
        
           return v
    }

Thanks for your help

2

There are 2 answers

0
Camille Basbous On

For anyone wondering use car.cliptobounds=true

0
Sardorbek Ruzmatov On

Try adopting the "delegate" protocol and implement this exact method:

    func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
        switch option {
        case .visibleItems:
            return 3
        default:
            return value
        }
    }