0

I have created a stackview in storyboard which is having three buttons and I want to insert a separator in between all these three buttons, so I have created a separator blank view programmatically with frame CGRect(x: 0, y: 0, width: 1, height: 12) having gray colour. I am trying to insert this view in between all three buttons. But it's not working properly. I have option to add this separator on storyboard and hide-show that and it will work. But my buttons are dynamic in number so I want to insert separator programmatically in stackview.

How can we insert any view/any UI element programmatically in storyboard designed Stack view?

Storyboard:

enter image description here

Code:

@IBOutlet weak var mainStack: UIStackView!

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    let sep1 = getSeparatorView()
    let sep2 = getSeparatorView()
    let sep3 = getSeparatorView()
    mainStack.insertSubview(sep1, at: 1)
    mainStack.insertSubview(sep2, at: 3)
}

 func getSeparatorView() -> UIView {
        let separator = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 12))
        separator.backgroundColor = UIColor.lightGray
        return separator
    }

Note:

I tried by inserting insertArrangedSubview function but it looks like this (only one separator added with wrong width):

mainStack.insertArrangedSubview(sep1, at: 1)
        mainStack.insertArrangedSubview(sep2, at: 3)

enter image description here

2
  • 1
    Don't add it as a sub view, add it to the arrangedSubviews array Commented Dec 18, 2024 at 10:13
  • @Paulw11 I tried by arrangedSubviews as well. It's not looking properly still. Edited question, please check. Commented Dec 18, 2024 at 10:18

1 Answer 1

2

There is a misunderstanding here. The stackView in Storyboard was designed with auto layout, on the other hand, separator is using frame. It cannot be compatible. So:

  1. As Paulw said, arrangedSubviews is the correct function.
mainStack.insertArrangedSubview(sep1, at: 1)
mainStack.insertArrangedSubview(sep2, at: 3)
  1. Convert the separator's frame to constraints.
func getSeparatorView() -> UIView {
    ...
    separator.translatesAutoresizingMaskIntoConstraints = false
    separator.addConstraints([
        .init(item: separator, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 1),
        .init(item: separator, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 12)
    ])
    return separator
}
  1. Change the stack properties for better layout, you can read the documentation here.
mainStack.spacing = 8
mainStack.distribution = .fill
mainStack.alignment = .center

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Nice, clear answer. (Voted). Why did you make the separator in your answer 8 points wide though? It looks like the OP was trying for a 1-point-wide separator.
@DuncanC thank you. All I want to do is increase its visibility. Btw, I edited the answer xD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.