文本与TextView swift中的自定义按钮重叠

问题描述 投票:0回答:1

问题:我正在尝试为我的应用构建一个“评论”组件。我已经设法创建了一个textview,随着更多文本的输入而扩展,并且还在textview中创建了一个始终位于右上角的按钮。但是,当我写文本时,文本将在按钮下面并且不可见。我想要的是文本不与按钮重叠,而是远离按钮。

问题我怎样才能实现这一目标?

enter image description here

这是我的代码:

class ClickedOnPostViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {

    var answersOf: Answer?


    @IBOutlet weak var tableView: UITableView!

    @IBOutlet weak var commentText: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()


        commentText.translatesAutoresizingMaskIntoConstraints = false
        [

            commentText.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            commentText.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            commentText.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            commentText.heightAnchor.constraint(equalToConstant: 43)
            ].forEach{ $0.isActive = true }


        commentText.addSubview(button)

        button.heightAnchor.constraint(equalToConstant: 50).isActive = true
        button.widthAnchor.constraint(equalToConstant: 100).isActive = true
        button.topAnchor.constraint(equalTo: commentText.topAnchor).isActive = true
        button.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        view.bringSubview(toFront: button)


        commentText.delegate = self
        commentText.isScrollEnabled = false


        //Keyboard listeners

        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)

        //  qNotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)





        // Do any additional setup after loading the view.
    }

    let button: UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = .orange
        button.setTitle("Click Me", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()



    @objc func keyboardWillShow(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            if self.view.frame.origin.y == 0 {
                self.view.frame.origin.y -= keyboardSize.height
            }
        }
    }

    @objc func keyboardWillHide(notification: NSNotification) {
        if self.view.frame.origin.y != 0 {
            self.view.frame.origin.y = 0
        }
    }



    @IBAction func refresh(_ sender: Any) {
    }


    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            commentText.resignFirstResponder()
            return false
        }
        return true
    }

    func textViewDidChange(_ textView: UITextView) {

        let size = CGSize(width: view.frame.width, height: .infinity)
        let estimatedSize = textView.sizeThatFits(size)

        textView.constraints.forEach { (constraints) in
            if constraints.firstAttribute == .height {

                constraints.constant = estimatedSize.height

            }
        }

    }

}

更新Sh_Khan当前答案的问题:

在键入将重叠按钮的最后一个字符之前

键入最后一个将重叠按钮的字符后

enter image description here

swift xcode uibutton uitextview
1个回答
0
投票

你需要

commentText.translatesAutoresizingMaskIntoConstraints = false
button.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(commentText)
view.addSubview(button)

NSLayoutConstraint.activate([ 
    commentText.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
    commentText.leadingAnchor.constraint(equalTo: view.leadingAnchor), 
    commentText.heightAnchor.constraint(equalToConstant: 43),
    button.heightAnchor.constraint(equalToConstant: 50),
    button.widthAnchor.constraint(equalToConstant: 100),
    button.topAnchor.constraint(equalTo: commentText.topAnchor),
    button.trailingAnchor.constraint(equalTo: view.trailingAnchor),
    button.leadingAnchor.constraint(equalTo: commentText.trailingAnchor,constant:20)
])

commentText.delegate = self
commentText.isScrollEnabled = false

在使用带有trailingAnchor的rightAnchor时,不要将RTL与LTR逻辑混合使用

如果注释文本域的UI是在storyboard / xib中构建的,请不要再为其设置约束


要使textView = 80%的宽度,请将其删除

button.heightAnchor.constraint(equalToConstant: 50),

并添加此

commentText.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier:0.8,constant:-20),
© www.soinside.com 2019 - 2024. All rights reserved.