Swift iOS -ScrollView w / StackView PageController崩溃

时间:2017-12-21 03:58:39

标签: ios swift uiscrollview uipagecontrol uistackview

  1. 我跟着这个programmatic scrollview-pageview tutorial我一直在Scrollview代表ScrollViewDidScroll(...)
  2. 中遇到崩溃
      

    主题1:EXC_BREAKPOINT(代码= 1,子代码= 0x1030b0d98)

    此行显示崩溃:pageControl.currentPage = Int(round(pageFraction))

    为什么这种崩溃不断发生

    ViewController类:

    class ViewController: UIViewController, UIScrollViewDelegate {
    
    let scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.backgroundColor = UIColor.white
        return scrollView
    }()
    
    let stackView: UIStackView = {
        let stackView = UIStackView()
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.distribution = .equalSpacing
        return stackView
    }()
    
    let pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.translatesAutoresizingMaskIntoConstraints = false
        pageControl.currentPage = 0
        pageControl.tintColor = UIColor.white
        pageControl.pageIndicatorTintColor = UIColor.gray
        pageControl.currentPageIndicatorTintColor = UIColor.red
        pageControl.addTarget(self, action: #selector(pageControlTapped(sender:)), for: .valueChanged)
        return pageControl
    }()
    
    var views = [UIView]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        view.backgroundColor = UIColor.white
    
        scrollView.delegate = self
    
        configureViewsForViewArray()
        setScrollViewAnchors()
        setStackViewAnchors()
        setPageViewsInsideStackView()
        setPageControlAnchors()
    }
    
    func configureViewsForViewArray(){
    
        let pageView1 = PageView(headerText: "Header 1", paragraphText: "Bla bla bla", backgroundColor: .red)
        views.append(pageView1)
        let pageView2 = PageView(headerText: "Header 2", paragraphText: "Bla bla bla", backgroundColor: .orange)
        views.append(pageView2)
        let pageView3 = PageView(headerText: "Header 3", paragraphText: "Bla bla bla", backgroundColor: .blue)
        views.append(pageView3)
        let pageView4 = PageView(headerText: "Header 4", paragraphText: "Bla bla bla", backgroundColor: .green)
        views.append(pageView4)
    }
    
    func setScrollViewAnchors(){
    
        view.addSubview(scrollView)
    
        if #available(iOS 11.0, *) {
            scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
        } else {
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
        }
    
        if #available(iOS 11.0, *) {
            scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
        } else {
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
        }
    
        if #available(iOS 11.0, *) {
            scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
        } else {
            scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
        }
    
        if #available(iOS 11.0, *) {
            scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
        } else {
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
        }
    }
    
    func setStackViewAnchors(){
    
        scrollView.addSubview(stackView)
        stackView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
        stackView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
        stackView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
        stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
    }
    
    func setPageViewsInsideStackView(){
    
        for eachView in self.views{
            eachView.translatesAutoresizingMaskIntoConstraints = false
            stackView.addArrangedSubview(eachView)
            eachView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
            eachView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        }
    }
    
    func setPageControlAnchors(){
    
        view.addSubview(pageControl)
    
        pageControl.numberOfPages = views.count
    
        if #available(iOS 11.0, *) {
            pageControl.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
        } else {
            // Fallback on earlier versions
            pageControl.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        }
    
        if #available(iOS 11.0, *) {
            pageControl.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20).isActive = true
        } else {
            // Fallback on earlier versions
            pageControl.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20).isActive = true
        }
    }
    
    @objc func pageControlTapped(sender: UIPageControl) {
    
        let pageWidth = scrollView.bounds.width
        let offset = sender.currentPage * Int(pageWidth)
        UIView.animate(withDuration: 0.33, animations: { [weak self] in
            self?.scrollView.contentOffset.x = CGFloat(offset)
        })
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
        let pageWidth = scrollView.bounds.width
        let pageFraction = scrollView.contentOffset.x/pageWidth
    
        //I get a crash here?
        pageControl.currentPage = Int(round(pageFraction))
    }
    }
    

    //这是在

    上面的[UIView]数组中添加的视图的模型

    浏览量:

    import Foundation
    import UIKit
    
    class PageView: UIView {
    
        // Private so that it can only be modified from within the class
        private var headerTextField = UITextField()
        // When this property is set it will update the headerTextField text
        var headerText: String = "" {
            didSet {
                headerTextField.text = headerText
            }
        }
    
        // Private so that you can only change from within the class
        private var paragraphTextView = UITextView()
        // When this property is set it will update the paragraphTextView text
        var paragraphText: String = "" {
            didSet {
                paragraphTextView.text = paragraphText
            }
        }
    
        // Designated Init method
        init(headerText: String, paragraphText: String, backgroundColor: UIColor) {
            super.init(frame: .zero)
            setup()
            self.headerTextField.text = headerText
            self.paragraphTextView.text = paragraphText
            self.backgroundColor = backgroundColor
    
        }
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            setup()
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        func setup() {
            // Basic text and view setup
            headerTextField.isUserInteractionEnabled = false
            headerTextField.textColor = .black
            headerTextField.textAlignment = .center
            headerTextField.sizeToFit()
    
            paragraphTextView.isUserInteractionEnabled = false
            paragraphTextView.textColor = .black
            paragraphTextView.textAlignment = .center
            paragraphTextView.sizeToFit()
            paragraphTextView.isScrollEnabled = false
            paragraphTextView.backgroundColor = .clear
    
            // Configuring the textfield/view for autoLayout
            headerTextField.translatesAutoresizingMaskIntoConstraints = false
            self.addSubview(headerTextField)
            paragraphTextView.translatesAutoresizingMaskIntoConstraints = false
            self.addSubview(paragraphTextView)
    
            // Creating and activating the constraints
            NSLayoutConstraint.activate([
                headerTextField.centerXAnchor.constraint(equalTo: self.centerXAnchor),
                headerTextField.centerYAnchor.constraint(equalTo: self.centerYAnchor),
                paragraphTextView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
                paragraphTextView.topAnchor.constraint(equalTo: headerTextField.bottomAnchor, constant: 20),
                paragraphTextView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: (2/3))
                ])
        }
    
    }
    

0 个答案:

没有答案