简体   繁体   中英

UIView is not showing in UIScrollView programmatically

I have a UIView container inside UIScrollView. 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

At 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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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