简体   繁体   English

彼此内部有多个滚动视图

[英]Multiple scrollviews inside each other

I have a vertical paged scrollview inside a horizontal scrollview. 我有一个水平水平滚动视图里面的垂直页面滚动视图。 The horizontal scrollview works but the vertical one does not. 水平滚动视图有效,而垂直滚动视图无效。

I used scrollView.contentSize=CGSize(500,2000) on vertical scrollview but it is still not working. 我在垂直scrollview上使用了scrollView.contentSize=CGSize(500,2000) ,但仍无法正常工作。

Now what is the problem? 现在是什么问题?

I should say, the vertical one added to view, programmatically using xib. 我应该说,垂直添加到视图中,以编程方式使用xib。

this is my Main.swift (i have no storyboard): 这是我的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)
}
}

and my TourDetailsDescription.swift : 和我的TourDetailsDescription.swift

class TourDetailsDescription: UIView { 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
}
}

May be this help you :), this is working on playground 可能对您有帮助:),正在操场上工作

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM