UIScrollView不在UINavigationItem中显示

时间:2016-12-15 07:37:58

标签: ios uiscrollview uinavigationitem

在我的UIView子类中,我添加了下面的init方法,然后我将此视图指定为navigationItem上的titleView。我看到一个红色矩形,意味着这个视图在那里,但我没有看到黄色滚动视图。我在这里做错了什么?

open class ChatNavBarImagesView : UIView {
    private let scrollView = UIScrollView()

    public init(navigationController: UINavigationController) {
        let height = navigationController.navigationBar.frame.height

        super.init(frame: CGRect(x: 0, y: 0, width: 200, height: height))

        backgroundColor = UIColor.red

        addSubview(scrollView)

        scrollView.leadingAnchor.constraint(equalTo: leadingAnchor)
        scrollView.trailingAnchor.constraint(equalTo: trailingAnchor)
        scrollView.topAnchor.constraint(equalTo: topAnchor)
        scrollView.bottomAnchor.constraint(equalTo: bottomAnchor)

        scrollView.backgroundColor = UIColor.yellow
        scrollView.contentSize = CGSize(width: 200, height: height)
    }

1 个答案:

答案 0 :(得分:0)

您需要将scrollView的框架设置为ChatNavBarImagesView

的边界
scrollView.frame = bounds
addSubview(scrollView)