Jayaraj
Jayaraj

Reputation: 342

Unable to add button on top right inside UITextView programmatically in swift

the textview is dynamic, which contains a button on top right corner. it works for textfield. not showing button in textview..

Awaiting....

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

}

Upvotes: 1

Views: 1261

Answers (2)

JeeVan TiWari
JeeVan TiWari

Reputation: 335

See for the proper Constraints rightAnchor to textView not working give it self.view.rightAnchor

enter image description here

Black Part is textView and Orange part is Button

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



}

Upvotes: 0

Fangming
Fangming

Reputation: 25260

It's too complicated to deal with NSLayoutConstraint. Instead, try this simple Anchor layout code. Replace everything under self.addSubView to these simple five lines of code

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

Upvotes: 1

Related Questions