ios - 在不滚动的情况下在 ScrollView 上添加 View (以编程方式)

标签 ios swift swift3 uiscrollview uipagecontrol

我正在尝试在我的 UIScrollView 自定义类之上添加一个页面控件。一切正常。但是每当我滑动 scrollView 时,Page Control 也会沿着第一个图像滑动。

这是我的代码:

import Foundation
import UIKit
import SwiftyGif

class MBSPagingScrollView: UIScrollView {

var pageControl = UIPageControl()
var imagesArray = [String]()

override init (frame : CGRect) {
    super.init(frame : frame)
}

convenience init () {
    self.init(frame:CGRect.zero)
}

required init(coder aDecoder: NSCoder) {
    fatalError("This class does not support NSCoding")
}

init(frame : CGRect, imagesarray : [String]){
    super.init(frame : frame)

    imagesArray = imagesarray

    self.isPagingEnabled = true
    self.showsHorizontalScrollIndicator = false

    for (index, imageName) in imagesArray.enumerated() {

        var imageView = UIImageView()

        if imageName.hasSuffix(".gif") {
            let gifManager = SwiftyGifManager(memoryLimit:30)
            let gif = UIImage(gifName: imageName)
            imageView = UIImageView(gifImage: gif, manager: gifManager)
        }

        else {
            imageView = UIImageView(image: UIImage(named: imageName))
            imageView.contentMode = UIViewContentMode.scaleToFill
        }

        imageView.frame = CGRect(x: CGFloat(index) * self.frame.size.width, y: 0, width: self.frame.size.width, height: self.frame.size.height)
        self.addSubview(imageView)

    }

    self.contentSize = CGSize(width:self.frame.size.width * CGFloat(imagesArray.count),height: self.frame.size.height)

    configurePageControl()
    pageControl.addTarget(self, action: #selector(self.changePage(sender:)), for: UIControlEvents.valueChanged)
}

func configurePageControl() {
    self.pageControl.frame = CGRect(x: 0, y: self.frame.size.height - 50, width: self.frame.size.width, height: 50)
    self.pageControl.numberOfPages = imagesArray.count
    self.pageControl.currentPage = 0
    self.pageControl.tintColor = UIColor.red
    self.pageControl.pageIndicatorTintColor = UIColor.black
    self.pageControl.currentPageIndicatorTintColor = MainOrangeColor
    self.addSubview(pageControl)
}

//    // MARK : TO CHANGE WHILE CLICKING ON PAGE CONTROL
    func changePage(sender: AnyObject) -> () {
        let x = CGFloat(pageControl.currentPage) * self.frame.size.width
        self.setContentOffset(CGPoint(x:x, y:0), animated: true)
    }

func selfDidEndDecelerating(_ scrollView: UIScrollView) {
    let pageNumber = round(self.contentOffset.x / self.frame.size.width)
    pageControl.currentPage = Int(pageNumber)
}

这里是问题发生的图片

Page control is centered on the first image

enter image description here

如您所见,页面控件随第一张图片一起滑动。我需要它留在中心。

如有任何帮助,我们将不胜感激!

最佳答案

我想你可以通过转到 Storyboard 来做到这一点,然后从 UI Builder 的左侧菜单中,向下拖动你的页面控件并将其放在 ScrollView 之外并放在它下面,如下图所示:

enter image description here

然后您需要添加合适的约束以正确放置您的页面控件。

祝你好运!

关于ios - 在不滚动的情况下在 ScrollView 上添加 View (以编程方式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45765650/

相关文章:

ios - 如何本地化 iOS 图标

ios - Swift Firebase metaData!.downloadURL()!.absoluteString

swift - 创建 Xcode 7 中缺少的新 cocoa touch 类

ios - 类型 'NSDictionary!' 没有下标成员 Swift - 3.0 转换问题

ios - 如何在 swift 3 的 UITableView 中创建和使用变量

ios - 具有固定位置的元素在 Safari iPhone 上呈现缓慢

ios - 每分钟向 Apple Watch 发送数据

ios - [CLLocationCoordinate2d]?没有名为下标的成员

swift - TableView 分隔符单元格消失?

swift - 自定义 View 不符合协议(protocol) UICollectionViewDataSource