彼此内部有多个滚动视图

时间:2017-11-15 15:19:42

标签: ios swift scrollview

我在水平滚动视图中有一个垂直分页滚动视图。 水平滚动视图有效,但垂直滚动视图不起作用。

我在垂直滚动视图上使用了scrollView.contentSize=CGSize(500,2000),但它仍无效。

现在问题是什么?

我应该说,使用xib以编程方式添加垂直视图。

这是我的 Main.swift (我没有故事板):

class test: UIViewController {

@IBOutlet weak var scroll: UIScrollView!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func viewDidLayoutSubviews() {

    let myView3:TourDetailsDescription = TourDetailsDescription(frame: CGRect(x: 0, y: 0, width: scroll.frame.width, height: scroll.frame.height))
    myView3.getView().frame.origin.x = CGFloat(2*scroll.frame.width)
    scroll.addSubview(myView3)

    let img1:UIImageView = UIImageView(image: UIImage(named: "kavir"))
    img1.frame = CGRect(x: 0, y: 0, width: 300, height: 600)
    myView3.mainScroll.addSubview(img1)
    myView3.mainScroll.contentSize = CGSize(width: 300, height: 600)


    self.scroll.contentSize = CGSize(width:UIScreen.main.bounds.width * CGFloat(2), height:self.scroll.frame.height)
}
}

和我的 TourDetailsDescription.swift

class TourDetailsDescription:UIView {

@IBOutlet weak var mainScroll: UIScrollView!
var parent:UIView!
@IBOutlet var contentView: UIView!

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

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commitInit()
}
private func commitInit(){
    Bundle.main.loadNibNamed("TourDetailsDescription", owner: self, options: nil)
    addSubview(contentView)
    contentView.frame = self.bounds
    contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}

func getView()->UIView{
    return contentView
}
}

1 个答案:

答案 0 :(得分:0)

可能会帮助你:),这是在游乐场上工作

class ScrollingViewController : UIViewController {

override func loadView() {

    let contentView = UIView(frame: CGRect(x: 0, y: 20, width: 2000, height: 200))
    contentView.backgroundColor = .green

    let scrollView = UIScrollView()
    scrollView.contentSize = contentView.frame.size
    scrollView.addSubview(contentView)
    scrollView.flashScrollIndicators()
    scrollView.backgroundColor = .white

    let verticalContainerView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    verticalContainerView.backgroundColor = .blue

    let verticalContentView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 2000))
    verticalContentView.backgroundColor = .red

    let verticalScrollView = UIScrollView(frame: verticalContainerView.frame)
    verticalScrollView.contentSize = verticalContentView.frame.size
    verticalScrollView.addSubview(verticalContentView)
    scrollView.flashScrollIndicators()
    verticalScrollView.backgroundColor = .white

    verticalContainerView.addSubview(verticalScrollView)

    contentView.addSubview(verticalContainerView)

    self.view = scrollView
}

}

PlaygroundPage.current.liveView = ScrollingViewController()