无法在swift中以编程方式在UITextView内部的右上角添加按钮

时间:2017-07-11 05:32:55

标签: objective-c swift uitextview

textview是动态的,右上角有一个按钮。它适用于文本字段。没有在textview中显示按钮..

...等待

class CommonTextView: UITextView {

private let microphoneButton = UIButton(type: .System)

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    initTextView()
}

func initTextView() -> Void {

    self.layer.cornerRadius = 7

    microphoneButton.translatesAutoresizingMaskIntoConstraints = false
    //microphoneButton.backgroundColor = UIColor.lightGrayColor().colorWithAlphaComponent(0.3)
    microphoneButton.setImage(UIImage(named: "mic"), forState: .Normal)
    microphoneButton.tintColor = UIColor.darkGrayColor()
    microphoneButton.addTarget(self, action: #selector(self.microphonePressed), forControlEvents: .TouchUpInside)
    self.addSubview(microphoneButton)

    let trailingConstraint = NSLayoutConstraint(item: microphoneButton, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: 0)
    let topConstraint = NSLayoutConstraint(item: microphoneButton, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0)
    let widthConstraint = NSLayoutConstraint(item: microphoneButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40)
    let heightConstraint = NSLayoutConstraint(item: microphoneButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40)
    self.addConstraints([trailingConstraint, topConstraint, widthConstraint, heightConstraint])

}

2 个答案:

答案 0 :(得分:1)

处理NSLayoutConstraint太复杂了。相反,尝试这个简单的Anchor布局代码。将self.addSubView下的所有内容替换为这些简单的五行代码

let margins = view.layoutMarginsGuide
microphoneButton.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: 0).isActive = true
microphoneButton.topAnchor.constraint(equalTo: margins.topAnchor, constant: 100).isActive = true
microphoneButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
microphoneButton.heightAnchor.constraint(equalToConstant: 30).isActive = true

答案 1 :(得分:0)

查看正确的约束rightAnchor到textView不工作给它self.view.rightAnchor

enter image description here

黑色部分是textView,橙色部分是按钮

 import UIKit

class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        view.addSubview(textView)
        textView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        textView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        textView.heightAnchor.constraint(equalToConstant: 200).isActive = true
        textView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true


        textView.addSubview(button)


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

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    let textView: UITextView = {
        let label = UITextView()
       // label.text = "Jeevan Chandra Tiwari"
        label.backgroundColor = .black
        label.textColor = .white
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

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



}