UIScrollView 没有滚动?
Posted
技术标签:
【中文标题】UIScrollView 没有滚动?【英文标题】:UIScrollView didn't scrolling? 【发布时间】:2017-05-23 07:15:54 【问题描述】:我正在从代码中创建一个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)
【问题讨论】:
【参考方案1】:像这样使用
override func viewDidLayoutSubviews()
MainScrollView?.contentSize = CGSize(width:self.view.bounds.width, height: 8000)
【讨论】:
【参考方案2】:似乎 in viewDidLoad 布局没有完成,所以 scrollView 不会滚动,因为它没有适当的 contentSize 尝试将 ViewWillAppear 中的 contentSize 设置为
MainScrollView?.contentSize = CGSize(width:self.view.bounds.width, height: 8000)
【讨论】:
以上是关于UIScrollView 没有滚动?的主要内容,如果未能解决你的问题,请参考以下文章
UIScrollView 不使用 RubyMotion 滚动