0

I'm trying to add checkboxes (library views) to container view. But they do not appear.

@IBOutlet weak var checkBoxesContainerView: UIView! //this is view where I', trying to add checkboxes

    func initialize() {
        let optionsList = webFormElement.possibleValues
        var latestCheckBox: Checkbox? = nil
        for option in optionsList {
            var checkBox = Checkbox(frame: CGRect(x: 0, y: 0, width: 20, height: 20)) //creating checkbox programmatically - it's custom view, but it's works in another places
            checkBox.borderLineWidth = 1
            checkBox.uncheckedBorderColor = UIColor.black
            checkBoxesContainerView.addSubview(checkBox)

            checkBox.translatesAutoresizingMaskIntoConstraints = false
            checkBox.widthAnchor.constraint(equalToConstant: 20).isActive = true
            checkBox.heightAnchor.constraint(equalToConstant: 20).isActive = true

            if(latestCheckBox == nil) {
                checkBox.topAnchor.constraint(equalTo: checkBoxesContainerView.topAnchor, constant: 0).isActive = true
                checkBox.leadingAnchor.constraint(equalTo: checkBoxesContainerView.leadingAnchor, constant: 0).isActive = true
            } else {
                checkBox.topAnchor.constraint(equalTo: latestCheckBox!.topAnchor, constant: 8).isActive = true
                checkBox.leadingAnchor.constraint(equalTo: latestCheckBox!.leadingAnchor, constant: 0).isActive = true
            }

            latestCheckBox = checkBox
        }
    }

Please help! No checkboxes at the screen (

Vladimir Fisher
  • 3,090
  • 2
  • 17
  • 23
  • You are showing how you constrain the "checkBoxes" to the container view, but you're not showing constraints for the container view itself. Most likely, it has ended up with a size of `0,0` and so nothing outside of it is visible. – DonMag Jan 14 '20 at 18:36
  • Where do you call func `initialize()`? Are you sure `optionsList` array is not empty? – David Steppenbeck Jan 14 '20 at 18:40
  • Make sure you're calling `initialize()`, and make sure `webFormElement.possibleValues` is not empty (otherwise there are no checkBoxes to create). Other than that, when `latestCheckBox` is not `nil`, you probably want the next constraint to be `equalTo: latestCheckBox!.bottomAnchor` (instead of `.topAnchor`), or your checkBoxes will overlap. – DonMag Jan 15 '20 at 13:40
  • I've tried without frame. The same result ( – Vladimir Fisher Jan 15 '20 at 20:48
  • @ВладимирФишер - put a `print(option)` statement as the first line after `for option in optionsList` and confirm that the code is actually executing. – DonMag Jan 16 '20 at 13:41

1 Answers1

-1

I think the problem is you are mixing Frame and Autolayout. Use either one but not both.

Try to initialize your Checkbox like this CheckBox() and add these along with widthAnchor and heightAnchor

checkBox.topAnchor.constraint(equalTo: checkBoxesContainerView.topAnchor).isActive = true

checkBox.leftAncho.constraint(equalTo: checkBoxesContainerView.leftAnchor).isActive = true
tuyen le
  • 305
  • 5
  • 11
  • As soon as this line: `checkBox.translatesAutoresizingMaskIntoConstraints = false` executes, the existing frame is ignored. The posted code is already setting width and height anchors, so instantiating the view with `CheckBox()` or `Checkbox(frame: CGRect(x: 0, y: 0, width: 20, height: 20))` doesn't matter. – DonMag Jan 15 '20 at 13:43
  • true, but he has `leadingAnchor`. This is common problem. It should be `leftAnchor`. And he should not mix frame and autolayout. It is confusing. – tuyen le Jan 15 '20 at 14:16
  • `leadingAnchor` is recommended over `leftAnchor` as it will cooperate with locales (RTL vs LTR). See this answer for a more complete description: https://stackoverflow.com/a/32981750/6257435 --- I do agree that mixing explicit frames with auto layout is a bad idea, but that is not what is causing his checkBox views to not show up. – DonMag Jan 15 '20 at 14:20
  • do you have any constraint for `checkBoxesContainerView`? Also, your `topAnchor` constant is 8. Are you sure it is not out of bound of `checkBoxesContainerView`? I would look at `checkBoxesContainerView` and make sure it has proper size and constraint. – tuyen le Jan 16 '20 at 03:17