简体   繁体   English

UIView 未以编程方式显示在 UIScrollView 中

[英]UIView is not showing in UIScrollView programmatically

I have a UIView container inside UIScrollView.我在 UIScrollView 中有一个 UIView 容器。 but it not showing in scrollview.但它没有显示在滚动视图中。 when I check in debug view hierarchy the width is ambiguous.当我检查调试视图层次结构时,宽度不明确。 here I show the pic and my code setup.在这里,我展示了图片和我的代码设置。

用户界面

        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


You must change your code to :您必须将代码更改为:

//your scroll view //你的滚动视图

在此处输入图片说明

//your navigationView //你的导航视图

在此处输入图片说明

在此处输入图片说明

And add to yourViewController : UIViewController, UIScrollViewDelegate并添加到yourViewController : UIViewController, UIScrollViewDelegate

At viewDidLoad :viewDidLoad :

yourScrollView.contentSize = CGSize(width: yourNavigationView.frame.width, height: yourNavigationView.frame.height)
    scroll.delegate = self

After then do whatever you want inside of your navigationView.然后在你的导航视图中做任何你想做的事情。

In addition, you can add this code if needs :此外,如果需要,您可以添加此代码:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView == scroll{
        if scrollView.contentOffset.x != 0 {
            scrollView.contentOffset.x = 0
        }
    }
}

Hope it helps...希望能帮助到你...

Going through programmatic way .通过程序化的方式。 ok here it is try using this好的,这是尝试使用它

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

    }
}

usage :用法 :

class ClientScrollViewController: BaseScrollViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func setupContainer(_ container: UIView) {
        //add your views here
    }
}

暂无
暂无

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

相关问题 UIView中的UIScrollView:以编程方式检测滚动 - UIScrollView in UIView: detecting scroll programmatically 想要在ios中以编程方式在UIView中实现UIPageController和UIScrollView - Want to implement UIPageController and UIScrollView in UIView programmatically in ios 不会以编程方式在Swift的UIViewcollection中显示UIView - UIView not showing in UIViewcollection in Swift, Programmatically 尝试使用Swift以编程方式显示UIScrollView而完全不显示 - Trying to show UIScrollView programmatically with Swift and not showing at all 选择以编程方式在UIScrollView中显示的当前按钮 - Select current button that is showing in UIScrollView programmatically 以编程方式滚动时在UIScrollView上显示滚动指示器 - Showing scroll indicators on a UIScrollView when programmatically scrolling 如何以编程方式在Swift中将UIView添加到UIScrollView? - How do I add a UIView to a UIScrollView In Swift programmatically? 以编程方式将UIScrollView添加到UIView在viewDidLoad中不起作用但在viewDidAppear中工作? - Add UIScrollView to UIView programmatically not work in viewDidLoad but work in viewDidAppear? 在 Swift 中以编程方式将 UIScrollView 滚动到子 UIView(子视图)的顶部 - Programmatically scroll a UIScrollView to the top of a child UIView (subview) in Swift 如何在 Swift 5 中的 UIScrollView 中以编程方式生成的 UIView 上实现点击手势 - How to Implement Tap Gesture on programmatically generated UIView inside UIScrollView in Swift 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM