scrollview无法正常工作,如果translatesAutoresizingMaskIntoConstraints = false

问题描述 投票:0回答:3

我在使用translatesAutoresizingMaskIntoConstraints时遇到一些问题。我正在创建一个滚动视图,您可以在其中向左或向右滑动。每次您获得另一个图像。但是由于某些原因,如果translatesAutoresizingMaskIntoConstraints = false,我无法滑动。

这是translatesAutoresizingMaskIntoConstraints = false(在这种情况下,我无法向左或向右滑动)

enter image description here

但是如果我设置了translatesAutoresizingMaskIntoConstraints = true,它看起来就像这样,我可以左右滑动。问题是布局被弄乱了。

enter image description here

我的代码:

var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.alwaysBounceVertical = false
        scrollView.alwaysBounceHorizontal = false
        scrollView.isPagingEnabled = true
        return scrollView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
scrollView.frame = contentView.frame
        contentView.addSubview(scrollView)

        scrollView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0).isActive = true
        scrollView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
        scrollView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0).isActive = true
        scrollView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
}

var frame = CGRect.zero
    func viewTutorial() {
        for i in 0..<arrayOfTutorilImages.count {
        frame.origin.x = scrollView.frame.size.width  * CGFloat((i))
            frame.size = scrollView.frame.size

            let imageView = UIImageView(frame: frame)
            imageView.image = UIImage(named: arrayOfTutorilImages[i])
            imageView.contentMode = .scaleAspectFit
            imageView.backgroundColor = .red
            imageView.translatesAutoresizingMaskIntoConstraints = false
            self.scrollView.addSubview(imageView)


            imageView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0).isActive = true
            imageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
            imageView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0).isActive = true
            imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true

        }

        scrollView.contentSize = CGSize(width: (scrollView.frame.size.width * CGFloat(arrayOfTutorilImages.count)), height: scrollView.frame.size.height)
        scrollView.delegate = self
    }

extension TutorialViewController: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
        pageControl.currentPage = Int(pageNumber)
    }
}
ios swift uiimageview uiimage scrollview
3个回答
0
投票

创建时添加滚动视图框架

var scrollView: UIScrollView = {
    let scrollView = UIScrollView(frame:.zero)
    //other properties
    return scrollView
}()

0
投票

ImageView约束与ScrollView相同。这就是为什么您无法滑动的原因,因为所有图像都具有相同的约束,并且彼此重叠。您应该这样做:

imageView.translatesAutoresizingMaskIntoConstraints = true
imageView.frame.size = contentView.frame.size
imageView.frame.origin = CGPoint(x: imageIndex * contentView.frame.size.width, y: 0.0)


0
投票

您可能需要添加约束,可以尝试

class TutorialViewController: UIViewController {

    var scrollView: UIScrollView = {
            let scrollView = UIScrollView()
            scrollView.showsHorizontalScrollIndicator = false
            scrollView.showsVerticalScrollIndicator = false
            scrollView.translatesAutoresizingMaskIntoConstraints = false
            scrollView.alwaysBounceVertical = false
            scrollView.alwaysBounceHorizontal = false
            scrollView.isPagingEnabled = true
            return scrollView
        }()

        override func viewDidLoad() {
            super.viewDidLoad()
    scrollView.frame = contentView.frame
            contentView.addSubview(scrollView)

            scrollView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0).isActive = true
            scrollView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
            scrollView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0).isActive = true
            scrollView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true

            viewTutorial()
    }


        func viewTutorial() {
            var con:UIView = scrollView
            for i in 0..<arrayOfTutorilImages.count {

                let imageView = UIImageView(frame: CGRect.zero)
                imageView.image = UIImage(named: arrayOfTutorilImages[i])
                imageView.contentMode = .scaleAspectFit
                imageView.backgroundColor = .red
                imageView.translatesAutoresizingMaskIntoConstraints = false
                self.scrollView.addSubview(imageView)

                if con == scrollView {
                     imageView.leftAnchor.constraint(equalTo: con.leftAnchor, constant: 0).isActive = true
                }
                else {
                     imageView.leftAnchor.constraint(equalTo: con.rightAnchor, constant: 0).isActive = true
                }

                imageView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true

                if i == arrayOfTutorilImages.count - 1 {
                    imageView.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: 0).isActive = true

                }
                imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0).isActive = true
                con = imageView

            }

            scrollView.delegate = self
        }
}

extension TutorialViewController: UIScrollViewDelegate {
     func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
         let pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width

     }
 }
© www.soinside.com 2019 - 2024. All rights reserved.