ios - 将 UIButton 放置在具有约束的 UIScrollView 底部

标签 ios swift constraints snapkit

我有一个包含三个文本字段/ TextView 字段的 View ,为了使 View 在键盘出现时可滚动,我将所有元素放入 ScrollView 中。元素之前不在框架之外,因此 contentSize 应该与全屏大小相同,然后当键盘出现时,因此当键盘出现时,我将 ScrollView 的底部约束更新为从 View 底部开始的 -keyboardHeight然后它可以在键盘上方滚动..

这一切都工作正常,问题是在 ScrollView 的底部添加一个按钮,该按钮引导/拖尾到 View 的侧面和底部。

看图片:

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

( super View / ScrollView 已设置为距两侧 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)
    }
}

有什么想法吗?

最佳答案

您需要首先在 scrollView subview 中添加一个 UIView (例如,将其命名为 containerView),然后将所有 uielement(例如,textfield/textview、UIButton)添加到该容器 View 的 subview 。

关于ios - 将 UIButton 放置在具有约束的 UIScrollView 底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50897507/

相关文章:

ios - 快速在导航栏下添加进度条?

ios - 继续使用 didSelectRowAtIndexPath

mysql - 更改 SQL 表上的 ondelete 行为

mysql - 添加外键时 Laravel/SQL 错误

ios - 不兼容的指针类型将“int”发送到“va_list”类型的参数(又名“char”)

ios - 无法加载测试包,因为出现意外错误

ios - Swift,检查数组是否在索引处有值

constraints - 在数据库中强制执行表间约束的最佳方法

ios - 从另一个 ViewController 在 subview 上调用 AnimateWithDuration 时出错

来自 Objective C 的 C++ 方法