in Education by
After editing my UIScrollView size, page control stopped working. You can see there are only 3 slides. But (1) Page Control isn't reflecting which slide I'm on and (2) I can only scroll from slide 1 to slide 3. Here's my code: func setupSlideScrollView(slides : [Slide]) { scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 154) scrollView.contentSize = CGSize(width: 188 * CGFloat(slides.count) + CGFloat((slides.count - 1) * 10), height: 153) scrollView.isPagingEnabled = true var lastCellMaxX: CGFloat = 0 let constantSpacingBetweenCell: CGFloat = 10 for i in 0 ..< slides.count { slides[i].frame = CGRect(x: lastCellMaxX, y: 0, width: 220, height: 153) scrollView.addSubview(slides[i]) lastCellMaxX += 188 + constantSpacingBetweenCell } } func scrollViewDidScroll(_ scrollView: UIScrollView) { let pageIndex = round(scrollView.contentOffset.x/scrollView.frame.size.width) pageControl.currentPage = Int(pageIndex) } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
func scrollViewDidScroll(_ scrollView: UIScrollView) { // This will always round down to zero // since the contentOffset.x will always be smaller than the frame width... let pageIndex = round(scrollView.contentOffset.x/scrollView.frame.size.width) // Possible logic to correct this? let pageIndex = scrollView.contentOffset.x / (width of a slide) pageControl.currentPage = Int(pageIndex) } -- Updated due to chat -- Your scrollview's frame is fine. However, you need to add a stackview to hold all your slides in order to scroll through it. Scrollviews are a bit tricky since you can't just add views and expect it to be scrollable. Here is test code I used to create a scrollView that contains stackviews. func createSlideshow() { scrollView.translatesAutoresizingMaskIntoConstraints = false let slide1 = UIView(frame: CGRect(x: 0, y: 0, width: 220, height: 375)) slide1.backgroundColor = UIColor.red let slide2 = UIView(frame: CGRect(x: 0, y: 0, width: 220, height: 375)) slide2.backgroundColor = UIColor.blue let slide3 = UIView(frame: CGRect(x: 0, y: 0, width: 220, height: 375)) slide3.backgroundColor = UIColor.yellow let slides: [UIView] = [slide1, slide2, slide3] let stackView = UIStackView() stackView.translatesAutoresizingMaskIntoConstraints = false stackView.axis = .horizontal stackView.alignment = .center stackView.distribution = .equalCentering stackView.spacing = 10 for slide in slides { stackView.addArrangedSubview(slide) slide.widthAnchor.constraint(equalToConstant: slide.frame.width).isActive = true slide.heightAnchor.constraint(equalTo: stackView.heightAnchor).isActive = true } scrollView.addSubview(stackView) stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true stackView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true stackView.widthAnchor.constraint(greaterThanOrEqualTo: scrollView.widthAnchor).isActive = true stackView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true }

Related questions

0 votes
    I tried to implement Ngrx into my Angular Application, but sadly haven't been able to retrieve any data. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    In My Activity there are more then one ListView. See the XML Layout of it: Now i am able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    In My Activity there are more then one ListView. See the XML Layout of it: Now i am able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    In My Activity there are more then one ListView. See the XML Layout of it: Now i am able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    In My Activity there are more then one ListView. See the XML Layout of it: Now i am able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    The Cookies Preferences box that always appears in the bottom right hand corner of the dataplatform.ibm.com ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I am the place where you create stories animation ,games etc in scratch Select the correct answer from above options...
asked Dec 23, 2021 in Education by JackTerrance
0 votes
    I have the following table relationship in my database: Parent / \ Child1 Child2 \ / GrandChild I am ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Here's the code: ''' file_path = (r'C:\Users\Luka\Desktop\Pyhton exercises\pi_digits.txt') with open( ... file_object: print(line) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Complete the concept map. Organizations reflecting multi polarity Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    i want to remove currencyViews (tableViews) that are not more needed. i can change dynamically them, and if ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    This is my code it's just starting the scan but it is not completing ,where is the error in it. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I am trying to implement the Hateoas using spring boot. In my UserController class i have used the below ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    i am in hurry , i want to sleep early; but i have a test for tomorrow, i have not study what can ido Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
...