1
votes

I’m trying to implement custom control according to official doc

The problem is that UIButton object placed into HorizontalStackVIew fills all its space ignoring its button constraints (width=4.0 height=4.0). (I tried with VerticalStackVIew and UITextView and other but it’s the same)

class MyControl: UIStackView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupButtons()
    }

    required init(coder: NSCoder) {
        super.init(coder: coder)
        setupButtons()
    }

    private func setupButtons() {

        // Create the button
        let button = UIButton()
        button.backgroundColor = UIColor.red

        // Add constraints
        button.translatesAutoresizingMaskIntoConstraints = false
        button.heightAnchor.constraint(equalToConstant: 4.0).isActive = true
        button.widthAnchor.constraint(equalToConstant: 4.0).isActive = true

        // Add the button to the stack
        addArrangedSubview(button)
    }

And I've got this log but don't know what to do with it:

2017-02-10 16:59:31.900999 MyControl[1835:91456] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "", "", "", "" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 2017-02-10 16:59:31.950461 MyControl[1835:91456] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "", "", "", "" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

Xcode 8.2.1

enter image description here

2

2 Answers

-1
votes

You can try this:

class CustomStackView:UIStackView{

 override init(frame:CGRect){
    super.init(frame: frame)
 }

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

   func setUpButton(){
     let button:UIButton = UIButton(type: .custom)
     button.backgroundColor = .red
     self.distribution = .fill
     self.addArrangedSubview(button)
   }

}

More details about stackview here:

-1
votes

I succeeded to create UIStackView object and put view (button) into it only programmatically (creating it by drag-drop on storyboard from Object library failed)

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let stackView = UIStackView()

        stackView.translatesAutoresizingMaskIntoConstraints = false

                let button = UIButton()
                button.translatesAutoresizingMaskIntoConstraints = false
                button.setTitle("Button", for: .normal)
                button.backgroundColor = .red
                stackView.addArrangedSubview(button)

        view?.addSubview(stackView)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

enter image description here