水平ScrollView不适合其子ImageViews?

时间:2018-03-30 07:33:46

标签: ios swift horizontalscrollview autoscroll

我想要水平滚动视图和图像视图,但图像不适合滚动。我正在做这个代码,但没有得到正确的事情 这是我的代码

text/html

i am getting this但我想要type

任何人都可以帮助我吗?感谢

3 个答案:

答案 0 :(得分:1)

根据您的要求,xOffset += 100应更改为xOffset += scrollWidth。内容大小设置为:

upperScroll.contentSize = CGSize(width: CGFloat((scrollWidth * logoImage.count)), height: 110)

请记住为滚动视图启用分页。

答案 1 :(得分:1)

只需删除 appdelegate 代码并添加我的功能即可 还设置了滚动视图约束,如前导,尾随,顶部,底部零

  func scrollImage() {
            super.viewDidLoad()
            self.navigationController?.navigationBar.isHidden = true
            // Do any additional setup after loading the view.
            var logoImage: [UIImage] = [
            UIImage(named: "slider.png")!,
            UIImage(named: "slider.png")!,
            UIImage(named: "slider.png")!,
            UIImage(named: "slider.png")!]

            upperScroll.isPagingEnabled = true

            upperScroll.isScrollEnabled = true

            let scrollWidth: Int = Int(self.view.frame.width)
,
            upperScroll.contentSize = CGSize(width: CGFloat(scrollWidth), height:(self.upperScroll.frame.height))
            print(upperScroll.frame.size.width)
            upperScroll.backgroundColor = UIColor.red
            for index in 0..<logoImage.count {
                let xPosition = self.view.frame.width * CGFloat(index)
                upperScroll.layoutIfNeeded()
                let img = UIImageView(frame: CGRect(x: CGFloat(xPosition), y: 0, width: upperScroll.frame.size.width, height: self.upperScroll.frame.height))
                img.layoutIfNeeded()
                img.image = logoImage[index]
                upperScroll.addSubview(img)
            }
            view.addSubview(upperScroll)
            upperScroll.contentSize = CGSize(width: CGFloat((scrollWidth) * logoImage.count), height: self.upperScroll.frame.height)

        }

我希望它能节省您的时间并且工作得很好

答案 2 :(得分:1)

首先,滚动视图和PageControl需要两个插座,

 var contentWidth:CGFloat = 0.0

// MARK: - Welcome screen with scroll view use page controll

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    scrollView.delegate = self
    let myImages = ["intro1_1.png", "intro2_2.png", "intro3_3.png"] // images which loaded in assets
    let imageWidth:CGFloat = view.frame.width
    let imageHeight:CGFloat = view.frame.height
    var xPosition:CGFloat = 0 //setup your position
    var scrollViewSize:CGFloat=0 //setup your position

    for image in myImages {
        let myImage:UIImage = UIImage(named: image)!
        let myImageView:UIImageView = UIImageView()
        myImageView.image = myImage
        myImageView.frame.size.width = imageWidth
        myImageView.frame.size.height = imageHeight
        myImageView.frame.origin.x = xPosition
        myImageView.frame.origin.y = 0
        scrollView.addSubview(myImageView)
        xPosition += imageWidth
        scrollViewSize += imageWidth
    }
    scrollView.contentSize = CGSize(width: scrollViewSize, height: imageHeight)
    scrollView.contentSize = CGSize(width: view.frame.width * 3, height: view.frame.height) //here you setup how many pages in scroll will

}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let pageWidth:CGFloat = scrollView.frame.width
    let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth / 2)/pageWidth)+1
    self.pageControll.currentPage = Int(currentPage)
}

希望,我帮助你