UIView 未以编程方式显示在 UIScrollView 中
Posted
技术标签:
【中文标题】UIView 未以编程方式显示在 UIScrollView 中【英文标题】:UIView is not showing in UIScrollView programmatically 【发布时间】:2020-02-28 11:02:49 【问题描述】:我在 UIScrollView 中有一个 UIView 容器。但它没有显示在滚动视图中。当我检查调试视图层次结构时,宽度是不明确的。在这里我展示了图片和我的代码设置。
view.addSubview(scrollView)
scrollView.contentSize.height = 1000
scrollView.backgroundColor = #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
scrollView.anchor(top: view.topAnchor, trailing: view.trailingAnchor, bottom: view.bottomAnchor, leading: view.leadingAnchor, topPadding: 0, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: 0, height: 0)
scrollView.addSubview(navigationView)
navigationView.addSubview(titleLbl)
navigationView.addSubview(profileIV)
navigationView.backgroundColor = .red
navigationView.anchor(top: scrollView.topAnchor, trailing: scrollView.trailingAnchor, bottom: nil, leading: scrollView.leadingAnchor, topPadding: 0, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: 0, height: 200)
titleLbl.anchor(top: navigationView.topAnchor, trailing: nil, bottom: nil, leading: navigationView.leadingAnchor, topPadding: 40, rightPadding: 0, bottomPadding: 0, leftPadding: 16, width: 50, height: 23)
profileIV.anchor(top: navigationView.topAnchor, trailing: nil, bottom: nil, leading: nil, topPadding: 80, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: 68, height: 68)
profileIV.centerXAnchor.constraint(equalTo: navigationView.centerXAnchor).isActive = true
profileNameLbl.anchor(top: profileIV.bottomAnchor, trailing: nil, bottom: nil, leading: nil, topPadding: 10, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: 0, height: 27)
profileNameLbl.centerXAnchor.constraint(equalTo: navigationView.centerXAnchor).isActive = true
【问题讨论】:
【参考方案1】:通过程序化方式。好的,这是尝试使用这个
open class BaseScrollViewController: UIViewController
lazy var contentViewSize = CGSize(width: self.view.frame.width, height: self.view.frame.height + 100)
lazy var scrollView: UIScrollView =
let view = UIScrollView(frame: .zero)
view.backgroundColor = .white
view.frame = self.view.bounds
view.contentSize = contentViewSize
return view
()
lazy var containerView: UIView =
let v = UIView()
v.backgroundColor = .clear
v.frame.size = contentViewSize
return v
()
override open func viewDidLoad()
super.viewDidLoad()
view.addSubviews(scrollView)
view.backgroundColor = .white
scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
scrollView.addSubview(containerView)
setupContainer(containerView)
public func setupContainer(_ container: UIView)
用法:
class ClientScrollViewController: BaseScrollViewController
override func viewDidLoad()
super.viewDidLoad()
override func setupContainer(_ container: UIView)
//add your views here
【讨论】:
嘿,谢谢@RishabhShukla,它有效。我想我需要分析一下为什么你的代码可以工作,因为我认为像我的代码设置这样的常规代码通常可以工作。比如 view.addSubview(scrollView) 和 scrollView.addSubview(someView)【参考方案2】:您必须将代码更改为:
//你的滚动视图
//你的导航视图
并添加到yourViewController : UIViewController, UIScrollViewDelegate
viewDidLoad :
yourScrollView.contentSize = CGSize(width: yourNavigationView.frame.width, height: yourNavigationView.frame.height)
scroll.delegate = self
然后在你的导航视图中做任何你想做的事情。
此外,如果需要,您可以添加此代码:
func scrollViewDidScroll(_ scrollView: UIScrollView)
if scrollView == scroll
if scrollView.contentOffset.x != 0
scrollView.contentOffset.x = 0
希望对你有帮助...
【讨论】:
嗨@Picode 它实际上可以工作,但这不是我想要的。这样做有更好的方法吗?我喜欢我的导航高度是 200,而滚动视图内的导航视图。所以我可以滚动导航视图 将您的 navigationView 的高度设置为 200 :) 我做了但是,结果是 navigationView 没有滚动。 @Picode :)以上是关于UIView 未以编程方式显示在 UIScrollView 中的主要内容,如果未能解决你的问题,请参考以下文章
UIScroll 视图 + UIView + UITableview 问题