如何在JSQMessageView控制器中添加静态头

时间:2017-10-24 10:12:14

标签: ios swift jsqmessagesviewcontroller

如何添加标题,就像下面的图像一样,不应该使用视图控制器向上滚动。

enter image description here

1 个答案:

答案 0 :(得分:1)

要添加静态标题视图,请使用以下函数(Swift 4):

注意:我试图直接使用storyboard来放置这种标题视图。但是,它不会奏效。所以,最后我通过以编程方式添加来实现它。

func addHeaderView() {
        let selectableView = UIView(frame: CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 60))
        selectableView.backgroundColor = UIColor(red: 56.0/255.0, green: 120.0/255.0, blue: 222.0/255.0, alpha: 1.0)

        let btn: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        btn.setImage(UIImage(named:"ArrowBack"), for: .normal)
        btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        btn.tag = 1
        selectableView.addSubview(btn)

        let titleLabel = UILabel(frame: CGRect(x: btn.frame.size.width+5, y: 5, width: selectableView.frame.size.width-60, height: 18))
        titleLabel.textColor = UIColor.white
        titleLabel.text = "Winter event 2017"
        selectableView.addSubview(titleLabel)

        let subTitleLabel = UILabel(frame: CGRect(x: btn.frame.size.width+5, y: 30, width: 100, height: 16))
        subTitleLabel.textColor = UIColor.white
        subTitleLabel.text = "135 members"
        selectableView.addSubview(subTitleLabel)

        let btnOptions: UIButton = UIButton(frame: CGRect(x: selectableView.frame.size.width-60, y: 0, width: 50, height: 50))
        btnOptions.setImage(UIImage(named:"Settings"), for: .normal)
        btnOptions.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        btnOptions.tag = 2
        selectableView.addSubview(btnOptions)

        view.addSubview(selectableView)
    }

    //then make a action method :

    @objc func buttonAction(sender: UIButton!) {
        let btnsendtag: UIButton = sender
        if btnsendtag.tag == 1 {
            self.navigationController?.popViewController(animated: true)
        } else {
            askForSettings()
        }
    }

还将此行代码放在viewDidLoad()

self.collectionView?.collectionViewLayout.sectionInset = UIEdgeInsets(top: 80, left: 0, bottom: 0, right: 0)