gk103
gk103

Reputation: 377

Add label within UiView

I am trying to add a label within a UIView. The UIView has been added to the viewcontroller via storyboard.

Whenever I try adding the label (using text box) it adds it to the view controller and not within the uiview. Here is the code, where am I going wrong?

protocol AddContentUIViewDelegate: class {
    func meathodForAddingChip(name: String)
}

class AddContentUIView: UIView {

    weak var delegate:AddContentUIViewDelegate?

    func addLabelFunc(name: String) {
        delegate?.meathodForAddingChip( name: name)
    }

    func addSV(label: UILabel){
        self.addSubview(label)
    }
}

class TheVC: UIViewController, AddContentUIViewDelegate {

    @IBOutlet weak var searchTextField: SearchTextField!
    @IBOutlet weak var contentUIView: AddContentUIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        contentUIView.delegate = self

        //Test to add label
        contentUIView.addLabelFunc(name: item.title)
    }

    func meathodForAddingChip(name: String) {

        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))        
        label.textColor = .black
        label.center = CGPoint(x: 160, y: 284)
        label.textAlignment = .center
        label.text = name
        contentUIView.addSV(label: label)
    }
}

I am trying to get "John appleseed" to be where the green label is in this screenshot:

enter image description here

Upvotes: 0

Views: 1778

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

The problem is in this line

  label.center = CGPoint(x: 160, y: 284)

comment it as it out of frame bounds

Upvotes: 3

Related Questions