addSubview doesn't work in UIView - Swift Programmatically

2.6k Views Asked by At

I need to add a UIButton as a subview on a UIView but it actually doesn't appear at runtime. This is my code:

let moreButton : UIButton = {
    let button = UIButton()
    button.setImage(#imageLiteral(resourceName: "more"), for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

 override init(frame: CGRect) {
    super.init(frame: frame)
    
    addSubview(moreButton)
    moreButton.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
    moreButton.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    moreButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
    moreButton.widthAnchor.constraint(equalToConstant: 20).isActive = true
    
    
}

The button isn't added eventually to the view. I'm sure this is an easy fix but I can't wrap my head around it.

1

There are 1 best solutions below

2
Viktor G On

First of all, make sure you the viewController is showing anything since you're doing it without storyboards, check out this simple tutorial:

https://medium.com/better-programming/creating-a-project-without-storyboard-in-2020-and-without-swifui-82080eb6d13b

If the problem is with that UIButton, try to set up the subviews in viewDidLoad:

final class ViewController: UIViewController {

let cardView = CardView()

override func viewDidLoad() {
    super.viewDidLoad()
    
    view.addSubview(cardView)
    
    /// Constraints
    let margins = view.layoutMarginsGuide
    cardView.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
    cardView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
    cardView.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
    cardView.heightAnchor.constraint(equalToConstant: 30).isActive = true
    cardView.widthAnchor.constraint(equalToConstant: 20).isActive = true
}

}

final class CardView: UIView {

let moreButton : UIButton = {
    let button = UIButton()
    button.setTitle("Button title", for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
}

override init(frame: CGRect) {
    super.init(frame:frame)
    
    self.translatesAutoresizingMaskIntoConstraints = false
    
    self.addSubview(moreButton)
    
    /// Constraints
    let margins = self.layoutMarginsGuide
    moreButton.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
    moreButton.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
    moreButton.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
    moreButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
    moreButton.widthAnchor.constraint(equalToConstant: 20).isActive = true
}
}

I've set up margins for the constraints and also added a leadingAnchor constraint, that might have been the issue as well.