使用约束将UIButton置于UIScrollView的底部

时间:2018-06-17 14:19:53

标签: ios swift constraints snapkit

我有一个带有三个textfield / textview字段的视图,为了在键盘出现时使视图可滚动,我将所有元素放在一个scrollview中。不在框架之前的元素,所以contentSize应该与全屏尺寸相同,然后当键盘出现时,所以当键盘出现时我将视图底部的scrollview的底部约束更新为-keyboardHeight然后它可以在键盘上方滚动..

一切正常,问题是在滚动视图的底部添加一个按钮,该按钮指向/拖尾到视图的侧面和底部。

见图片:

Current view, with button hidden behind navigation bar

Where i would like the button to be. Approx 20 margin to right.left.bottom

我正在使用SnapKit设置我的约束,对于按钮我想做这样的事情:

    sharebutton.snp.makeConstraints { (make) in
        make.width.left.right.equalToSuperview()
        make.bottom.equalToSuperview().offset(-20)
    }

(超级视图/滚动视图已设置为边距为20边距)

//编辑:已添加代码 - >

override func viewDidLayoutSubviews() {
    scrollview.contentSize = CGSize(width: centerView.frame.width,      height: view.frame.height)
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "title"
    view.backgroundColor = .white

    view.addSubview(scrollview)

    scrollview.addSubview(sharebutton)
    scrollview.addSubview(subjectField)
    scrollview.addSubview(messageField)
    scrollview.addSubview(datepicker)
    scrollview.addSubview(addressTable)
    addressTable.dataSource = self
    addressTable.delegate = self
    addressTable.separatorStyle = .none
    addressTable.keyboardDismissMode = .onDrag
    addressTable.separatorStyle = .singleLineEtched

    subjectField.placeholder = "content"
    datepicker.setTitle("topbutton", for: .normal)
    datepicker.setTitleColor(.black, for: .normal)
    datepicker.setTitleColor(UIColor.ME.border, for: .highlighted)
    datepicker.titleLabel?.font = UIFont.ME.button
    datepicker.layer.borderWidth = 1
    datepicker.layer.cornerRadius = 3
    datepicker.layer.borderColor = UIColor.ME.border.cgColor
    datepicker.addTarget(self, action: #selector(showDates), for: .touchUpInside)

    sharebutton.setTitle("Share", for: .normal)
    sharebutton.backgroundColor = UIColor.ME.buttonMainBlue
    sharebutton.addTarget(self, action: #selector(shareClicked), for: .touchUpInside)

    scrollview.backgroundColor = .red
    scrollview.snp.makeConstraints { (make) in
        make.top.bottom.equalToSuperview()
        make.left.width.right.equalTo(centerView)
    }

    datepicker.snp.makeConstraints { (make) in
        make.top.equalToSuperview().offset(paddingGlobal)
        make.left.width.right.equalToSuperview()
        make.height.equalTo(50)
    }

    addressTable.snp.makeConstraints { (make) in
        make.top.equalTo(datepicker.snp.bottom)
        make.left.right.equalTo(datepicker)
        make.height.equalTo(200)
    }

    subjectField.snp.makeConstraints { (make) in
        make.top.equalTo(datepicker.snp.bottom).offset(20)
        make.left.right.equalToSuperview()
    }

    messageField.snp.makeConstraints { (make) in
        make.top.equalTo(subjectField.snp.bottom).offset(20)
        make.height.equalTo(200)
        make.left.right.equalToSuperview()
    }

    sharebutton.snp.makeConstraints { (make) in
        make.width.left.right.equalToSuperview()
        make.bottom.equalToSuperview().offset(-20)
    }
}

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

正如Govind所说,我可以通过将子视图放在容器视图中来解决。

答案 1 :(得分:0)

您需要先在UIView子视图中添加一个scrollView(例如,容器视图),然后将所有uielement(例如textfield / textview,UIButton)添加到该容器视图的subView中。

答案 2 :(得分:0)

使用StoryBoard和Constaint Xcode 11.5 iOS 13

StoryBoard View Controller Scene

Simulator Screen Shot