title: Add UILabel as subview of UITextField on top
body: <p>I am in the process of implementing a <code>UILabel</code> as a subview of a <code>UITextField</code> which will be shown right above the <code>UITextField</code> itself. The <code>UITextField</code> has a rounded border and what I would like to achieve is the <code>UILabel</code> to be shown over the border.</p>
  
  <p>Everything currently works as expected, but the <code>UILabel</code> is drawn <strong>behind</strong> the border of the UITextField. I want it to go "over" (above) the border so the white backgroundColor would be shown above part of the border and make the text more easily readible.</p>
  
  <pre><code>var priceTextField: CustomTextField = {
  
      let priceTextField = CustomTextField()
      priceTextField.layer.cornerRadius = 10.0
      priceTextField.layer.borderWidth = 1.0
      priceTextField.layer.borderColor = UIColor.darkGray.cgColor
      priceTextField.translatesAutoresizingMaskIntoConstraints = false
      priceTextField.font = UIFont.systemFont(ofSize: 15)
      priceTextField.textColor = .black
      priceTextField.text = "0"
      priceTextField.suffix = "EUR"
      priceTextField.suffixTextColor = .darkGray
      priceTextField.suffixSpacing = 2.0
      priceTextField.textAlignment = .center
      priceTextField.labelText = "Price"
  
      return priceTextField
  
  }()
  </code></pre>
  
  <p>In my CustomTextField class (subclass of UITextField):</p>
  
  <pre><code>public var labelText: String?
  
  var topLabel: UILabel = {
  
      let topLabel = UILabel()
      topLabel.translatesAutoresizingMaskIntoConstraints = false
      topLabel.textAlignment = .center
      topLabel.font = UIFont.systemFont(ofSize: 12)
      topLabel.textColor = .lightGray
      topLabel.backgroundColor = .white
      topLabel.numberOfLines = 1
      return topLabel
  
  }()
  
  func setupLabel() {
  
      self.addSubview(topLabel)
      topLabel.centerYAnchor.constraint(equalTo: self.topAnchor).isActive = true
      topLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
      topLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -20).isActive = true
      topLabel.text = labelText
  
  }
  </code></pre>
  
  <p>I call <code>setupLabel()</code> at the end of the <code>draw(_ rect: CGRect)</code> method of <code>UITextField</code> (because I work with this to show the EUR sign always behind the entered value).</p>
  
  <p>I have tried to play around with <code>bringSubviewToFront</code> and changing the <code>zPosition</code> of the layer of the <code>UILabel</code>, without success.</p>
  
  <p>It now looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/Imh12.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Imh12.png" alt=""></a></p>
  
  <p>How can I bring the text "above" the border on the top?</p>
  
  <p>EDIT: Tried Sh_Khan's solution, but it's still hidden behind the border.</p>
  
  <pre><code>import Foundation
  import UIKit
  
  public class CustomTextView: UIView, UITextFieldDelegate {
  
      public var labelText: String?
  
      var customTextField: CustomTextField = {
  
          let customTextField = CustomTextField()
          customTextField.translatesAutoresizingMaskIntoConstraints = false
          customTextField.font = UIFont.systemFont(ofSize: 15)
          customTextField.textColor = .black
          customTextField.textAlignment = .center
          customTextField.text = "0"
          customTextField.suffix = "EUR"
          customTextField.suffixTextColor = .lightGray
          customTextField.suffixSpacing = 2.0
  
          return customTextField
  
      }()
  
      var topLabel: UILabel = {
  
          let topLabel = UILabel()
          topLabel.translatesAutoresizingMaskIntoConstraints = false
          topLabel.font = UIFont.systemFont(ofSize: 12)
          topLabel.textColor = .darkGray
          topLabel.numberOfLines = 1
          topLabel.backgroundColor = .red
          topLabel.textAlignment = .center
  
          return topLabel
  
      }()
  
      override public init(frame: CGRect) {
  
          super.init(frame: frame)
          setupBorders()
  
      }
  
      public override func layoutSubviews() {
  
          setupViews()
  
      }
  
      func setupBorders() {
  
          self.layer.cornerRadius = 10.0
          self.layer.borderColor = UIColor.lightGray.cgColor
          self.layer.borderWidth = 1.0
  
      }
  
      func setupViews() {
  
          addSubview(topLabel)
  //        insertSubview(topLabel, aboveSubview: customTextField)
          insertSubview(customTextField, belowSubview: topLabel)
  
          customTextField.topAnchor.constraint(equalTo: topAnchor).isActive = true
          customTextField.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
          customTextField.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
          customTextField.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
  
          topLabel.centerYAnchor.constraint(equalTo: topAnchor).isActive = true
          topLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
          topLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10).isActive = true
  
          topLabel.text = labelText
  
      }
  
      public required init?(coder aDecoder: NSCoder) {
  
          super.init(coder: aDecoder)
          setupViews()
  
      }
  
  }
  </code></pre>