UIScrollView没有滚动?

时间:2017-05-23 07:15:55

标签: ios swift3 uiscrollview

我正在从代码创建一个UIScrollView。唯一的问题是滚动不起作用经过很多帖子但没有找到任何东西。 这是我的代码任何帮助大大赞赏。

class ShopDetailviewController : UIViewController {


var MainScrollView : UIScrollView?

let viewForScrollview : UIView = {

    let view = UIView()
    view.backgroundColor = UIColor.darkGray
    return view
}()

let shopNameandAddresView : UIView = {

   let view = UIView()
   view.backgroundColor = UIColor.black
   return view
}()

let timePaymentDetail : UIView = {

    let view = UIView()
    view.backgroundColor = UIColor.blue
    return view
}()


let shopImage : UIImageView = {

    let shopeImage = UIImageView()
    shopeImage.backgroundColor = UIColor.green
    return shopeImage

}()

let ratingStarLable : UILabel = {

    let ratingStratLable = UILabel()
    ratingStratLable.backgroundColor = UIColor.red
    ratingStratLable.text = "3.5"
    return ratingStratLable

}()

let shopName : UILabel = {

    let shopName = UILabel()
    shopName.backgroundColor = UIColor.gray
    shopName.text = "Kimling"
    return shopName

}()

let shopLocation : UILabel = {

    let shopLocation = UILabel()
    shopLocation.backgroundColor = UIColor.white
    shopLocation.text = "Kondhwa"
    return shopLocation

}()

let seprater : UIView = {

    let seprater = UIView()
    seprater.backgroundColor = UIColor.white
    return seprater
}()



override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    navigationController?.navigationBar.tintColor = .white
    view.backgroundColor = UIColor.white
}

override func viewDidLoad() {

    super.viewDidLoad()

    MainScrollView = UIScrollView()
    MainScrollView?.isScrollEnabled = true
    MainScrollView?.indicatorStyle = .default
    MainScrollView?.contentSize = CGSize(width:self.view.bounds.width, height: 8000)
    MainScrollView?.backgroundColor = UIColor.purple

    setupViews()
}

   func setupViews(){

    view.addSubview(MainScrollView!)
    MainScrollView?.addSubview(viewForScrollview)

    viewForScrollview.addSubview(shopImage)
    viewForScrollview.addSubview(shopNameandAddresView)
    viewForScrollview.addSubview(timePaymentDetail)

    addConstraintsWithFormat("H:|-0-[v0(414)]-0-|", views: MainScrollView!)
    addConstraintsWithFormat("V:|-0-[v0(8000)]", views: MainScrollView!)

    addConstraintsWithFormat("H:|-0-[v0(414)]-0-|", views: viewForScrollview)
    addConstraintsWithFormat("V:|-0-[v0(8000)]", views: viewForScrollview)


    addConstraintsWithFormat("V:|-0-[v0(1000)]-0-[v1(1000)]", views: shopImage,shopNameandAddresView)
    addConstraintsWithFormat("H:|-0-[v0]-0-|", views: shopImage)
    addConstraintsWithFormat("H:|-0-[v0]-0-|", views: shopNameandAddresView)



}


}

2 个答案:

答案 0 :(得分:2)

用作

 override func viewDidLayoutSubviews()
{
  MainScrollView?.contentSize = CGSize(width:self.view.bounds.width, height: 8000) 
}

答案 1 :(得分:0)

似乎viewDidLoad布局没有完成,因此scrollView不会滚动,因为它没有正确的contentSize尝试将ViewWillAppear中的contentSize设置为

MainScrollView?.contentSize = CGSize(width:self.view.bounds.width, height: 8000)