Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 无法在swift中以编程方式将按钮添加到UITextView的右上角_Objective C_Swift_Uitextview - Fatal编程技术网

Objective c 无法在swift中以编程方式将按钮添加到UITextView的右上角

Objective c 无法在swift中以编程方式将按钮添加到UITextView的右上角,objective-c,swift,uitextview,Objective C,Swift,Uitextview,textview是动态的,它在右上角包含一个按钮。它适用于textfield。未在文本视图中显示按钮 等待 class CommonTextView: UITextView { private let microphoneButton = UIButton(type: .System) required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) initTextView() } func in

textview是动态的,它在右上角包含一个按钮。它适用于textfield。未在文本视图中显示按钮

等待

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

}

处理NSLayoutConstraint太复杂了。相反,请尝试这个简单的锚定布局代码。将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

有关rightAnchor到textView不工作的适当约束,请参阅。view.rightAnchor

黑色部分为文本视图,橙色部分为按钮


请显示您的代码。我已经更新了代码。您可以单击按钮所在的一般位置吗?它有任何效果吗?按钮不可见且没有效果。但是同样的代码适用于textfieldJusto来澄清——你有一个UITextView,里面有一个按钮吗?还是在上面?你是在使用情节串连板,还是只是以编程的方式做所有事情?我已经用你的代码替换了。但按钮不可见。现在textfield也不可见了。@jayaraj那么问题不在于按钮是self。你能发布更多与按钮相关的代码吗?@jayaraj-Oh你想在文本视图中显示这个按钮吗?您是否尝试将顶部锚点设置为0?在我的回答中,我将其设置为100,如果您的文本视图短于100,它将在屏幕上显示
 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
    }()



}