I love using iCarousel in my swift projects but there is one thing that I could not manage to overcome; I want to use Visual language for layout of the views in my project but whenever I use visual formats for iCarousel, it does not work.
I noticed that the problem is TopMenuCarousel.translatesAutoresizingMaskIntoConstraints=false
attribute.
Whenever I disable this attribute, my visual format constraints are disabled for iCarousel, and whenever I enable it, the constraints works perfectly but my iCarousel wont scroll and stay still always.
Current code:
#import UIKit
import iCarousel
class Step2_HomePage: UIViewController,iCarouselDelegate,iCarouselDataSource {
let TopMenuCarouselCount = 5
var TopMenuCarousel = iCarousel()
override func viewDidLoad() {
super.viewDidLoad()
print("Step2HomePage icinde")
TopMenuCarousel = iCarousel(frame: CGRect())
view.addSubview(TopMenuCarousel)
// TopMenuCarousel.clipsToBounds = true
TopMenuCarousel.type = .Linear
TopMenuCarousel.dataSource = self
TopMenuCarousel.delegate = self
let views = [ "TopMenuCarousel": TopMenuCarousel ]
// 2
var allConstraints = [NSLayoutConstraint]()
let TopMenuCarouselTop = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-100-[TopMenuCarousel]",
options: [],
metrics: nil,
views: views)
allConstraints += TopMenuCarouselTop
let TopMenuCarouselHorizontal = NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-0-[TopMenuCarousel]-0-|",
options: [],
metrics: nil,
views: views)
allConstraints += TopMenuCarouselHorizontal
TopMenuCarousel.translatesAutoresizingMaskIntoConstraints=false
NSLayoutConstraint.activateConstraints(allConstraints)
// Do any additional setup after loading the view, typically from a nib.
}
func numberOfItemsInCarousel(carousel: iCarousel) -> Int {
print("carousel number")
return TopMenuCarouselCount
}
func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
print("carousel view icinde")
let tempView = UIView(frame: CGRect(x: 0, y: 0 , width: 20, height: 20))
tempView.backgroundColor = UIColor.blueColor()
return tempView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
#
Looks like you're not setting any height to your iCarousel object. Try changing your first Constraint to:
let TopMenuCarouselTop = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-100-[TopMenuCarousel(20)]",
options: [],
metrics: nil,
views: views)
Here is a full modified version of your original code. I made the views larger (yours were 20x20), and added some color to make it easier to see what's going on.
import UIKit
import iCarousel
class Step2_HomePage: UIViewController,iCarouselDelegate,iCarouselDataSource {
// array of colors to make it easy to see the individual Carousel views
let arrayOfColors = [ UIColor.blueColor(), UIColor.redColor(), UIColor.yellowColor(), UIColor.orangeColor(), UIColor.greenColor()]
let TopMenuCarouselCount = 5
var TopMenuCarousel = iCarousel()
override func viewDidLoad() {
super.viewDidLoad()
print("Step2HomePage icinde")
self.view.backgroundColor = UIColor.lightGrayColor()
// initialize the TopMenuCarousel object
TopMenuCarousel = iCarousel(frame: CGRect())
// add TopMenuCarousel to the view
view.addSubview(TopMenuCarousel)
// if clipsToBounds == true, TopMenuCarousel subviews will be clipped to the TopMenuCarousel frame
// default is false
// TopMenuCarousel.clipsToBounds = true
TopMenuCarousel.type = .Linear
TopMenuCarousel.dataSource = self
TopMenuCarousel.delegate = self
TopMenuCarousel.backgroundColor = UIColor.purpleColor()
let views = [ "TopMenuCarousel": TopMenuCarousel ]
var allConstraints = [NSLayoutConstraint]()
// position TopMenuCarousel 100 from the Top, with a Height of 200
let TopMenuCarouselTop = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-100-[TopMenuCarousel(200)]",
options: [],
metrics: nil,
views: views)
allConstraints += TopMenuCarouselTop
// set TopMenuCarousel to stretch the full Width of the view
let TopMenuCarouselHorizontal = NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-0-[TopMenuCarousel]-0-|",
options: [],
metrics: nil,
views: views)
allConstraints += TopMenuCarouselHorizontal
// this property *must* be set to false
TopMenuCarousel.translatesAutoresizingMaskIntoConstraints=false
NSLayoutConstraint.activateConstraints(allConstraints)
}
func numberOfItemsInCarousel(carousel: iCarousel) -> Int {
print("carousel number \(TopMenuCarouselCount)")
return TopMenuCarouselCount
}
func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
print("carousel view icinde", index)
// create a 200 x 160 view to add to TopMenuCarousel
let tempView = UIView(frame: CGRect(x: 0, y: 0 , width: 200, height: 160))
// give it one of the colors
tempView.backgroundColor = arrayOfColors[index % arrayOfColors.count]
return tempView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}