1

I want to create scrollview with radiobuttons/checkbox buttons. I tried different ways working with scrollview and stackview, but here are my issues:

ScrollView and StackView added in IB, buttons added programmatically

Problem: multiline works fine, but button frame doesn`t work

ScrollView, StackView, buttons added programmatically

Problem: multiline title and buttons dynamic height doesn't work; dynamic stackview height in scroll view works fine

Target view

Question: how can I make button depends on titleHeight works with scrollable stackview

private func testDynamicBtnHeight() {

    scrollView.translatesAutoresizingMaskIntoConstraints = false
    mVScrollContainer.addSubview(scrollView)

   mVScrollContainer.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView]|", options: .alignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))
    mVScrollContainer.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView]|", options: .alignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))


    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.axis = .vertical
    scrollView.addSubview(stackView)



  scrollView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[stackView]", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: ["stackView": stackView]))

    scrollView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[stackView]|", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: ["stackView": stackView]))

    for i in 0 ... 30 {
        var btn1 = RadioButtonMock.mockRadioButton(title: "A B C D", backgroundColor: UIColor.green)
        var btn2 = RadioButtonMock.mockRadioButton(title: " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  ", backgroundColor: UIColor.red)
        var btn3 = RadioButtonMock.mockRadioButton(title: "B B B B B B B", backgroundColor: UIColor.blue)

        stackView.addArrangedSubview(btn1)
        stackView.addArrangedSubview(btn2)
        stackView.addArrangedSubview(btn3)

        if i == 9 {
            let url = URL(string: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQacMIFnZ2xZ_ZH7CjdUzzqpsMHwLSiF98lGWVWVHy6Am5qm2mq")
            let data = try? Data(contentsOf: url!)                

            if let imageData = data {
                let ivImage = UIImageView()
                ivImage.image = UIImage(data: imageData)
                ivImage.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
                stackView.addArrangedSubview(ivImage)

            }

        }            
    }
    //mSvContainer.heightAnchor.constraint(equalToConstant: 400)
}

Radio button code:

static func mockRadioButton(title: String, backgroundColor: UIColor) -> RadioButton {
    NSLog(TAG + " mockRadioButton(title: String, backgroundColor: UIColor)")        

    // min requirements to display simple checkBox
    let rb = RadioButton()
    rb.setTitle(title, for: .normal)       
    rb.setTitleColor(.black, for: .normal)
    //cb.addTarget(self, action: #selector(checkBoxParamTap(sender:)), for: .touchUpInside)
    rb.isChecked = false
    rb.registerClickListener()        

    // title advanced
    rb.titleLabel?.numberOfLines = 0    //multiline        

    //debug
    rb.backgroundColor = backgroundColor        
    NSLog(TAG + "ccccc \(rb.bounds.size.height)")        

    //size
    //rb.heightAnchor.constraint(equalToConstant: 30).isActive = true
    //let contentSize = self.txtVDetails.sizeThatFits(self.txtVDetails.bounds.size)

    //rb.frame = CGRect(x: 10, y: 10, width: 200, height: 50)  //doesnt work
    rb.heightAnchor.constraint(equalToConstant: 40).isActive = true  //works fine

    //rb.sizeToFit()
    //rb.layoutIfNeeded()

    rb.contentEdgeInsets.top = 8
    //cb.contentEdgeInsets.right = 16
    rb.contentEdgeInsets.left = 8
    rb.contentEdgeInsets.bottom = 8

    return rb
    }
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70

0 Answers0