无法将子视图添加到scrollView的底部

时间:2017-08-31 12:31:23

标签: swift scrollview

所以,我正在尝试在scrollView底部添加一个按钮,但是按钮的位置并不像预期的那样,当我滚动到顶部时按钮在scrollView顶部的位置,这是我的代码:

    lazy var scrollView : UIScrollView = {
    let scrollV = UIScrollView()
    scrollV.translatesAutoresizingMaskIntoConstraints = false
    scrollV.alwaysBounceVertical = true
    scrollV.delegate = self
    scrollV.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    return scrollV
}()

在viewDidLoad中:

       view.addSubview(scrollView)
    scrollView.topAnchor.constraint(equalTo: self.topBar.bottomAnchor).isActive = true
    scrollView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
    scrollView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
    scrollView.contentSize = CGSize(width: self.view.frame.width, height: 1000)

最后,我将按钮添加到scrollView:

    scrollView.addSubview(myButton)
    myButton.centerXAnchor.constraint(equalTo: self.scrollView.centerXAnchor).isActive = true
    myButton.bottomAnchor.constraint(equalTo: self.scrollView.bottomAnchor).isActive = true
    myButton.widthAnchor.constraint(equalToConstant: 150).isActive = true
    myButton.heightAnchor.constraint(equalToConstant: 50).isActive = true

0 个答案:

没有答案