Pressing_Keys_24_7
Pressing_Keys_24_7

Reputation: 1863

Adjust Carousel View for Different Screen Sizes in Xcode for iOS

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

Answers (1)

Volkan Sonmez
Volkan Sonmez

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

Related Questions