Reputation: 1863
I have set up the following Carousel on my View Controller. I wanted to ask that how can I adjust the Carousel for different screen sizes as I don't have any physical constraints set up in my storyboard.? I have already set up Constraint class for different devices, but How can I link that class with constraints of the Carousel View? Appreciate your help!
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)
// Do any additional setup after loading the view.
}
Upvotes: 0
Views: 529
Reputation: 785
Do not call view.frame.size.width ondidload, it does not work, it returns stoaryboard size. Actually you want to device width. There are two options.
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)
// Do any additional setup after loading the view.
}
Option 1
override func viewDidLayoutSubviews() {
myCarousel.frame = CGRect(x: 0, y: 220, width: view.frame.size.width, height: 400)
}
Option 2
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(myCarousel)
myCarousel.dataSource = self
myCarousel.frame = CGRect(x: 0, y: 220, width: UIScreen.main.bounds.width, height: 400)
// Do any additional setup after loading the view.
}
Additional Option: i did library for carouselview. You can use it. https://github.com/sonmezvolkan/UIBannerView
Upvotes: 1