Overlapping Views in UIStackView

后端 未结 7 1685
时光说笑
时光说笑 2020-12-14 17:24

I have an horizontal stack view that I added arranged sub views to it (7 cells). Each one of the cells in the stack has a circular badge that exceeds the view boundaries (ne

7条回答
  •  天命终不由人
    2020-12-14 17:35

    Programmatically when you add the subview, add height and width constant constraints. In the end call StackView's setNeedsLayout. It will calculate and try to adjust the views with spacing

    After various attempts i am able to make it horizontally

     stackView.leadingAnchor.constraint(equalTo: stackScrollView.leadingAnchor).isActive = true
     stackView.trailingAnchor.constraint(equalTo: stackScrollView.trailingAnchor).isActive = true
     stackView.bottomAnchor.constraint(equalTo: stackScrollView.bottomAnchor).isActive = true
     stackView.heightAnchor.constraint(equalTo: stackScrollView.heightAnchor).isActive = true
    
     stackView.distribution = .equalSpacing
     stackView.spacing = 5
     stackView.axis = .horizontal
     stackView.alignment = .fill
    
    
     for i in 0 ..< images.count {
         let photoView = UIButton.init(frame: CGRect(x: 0, y: 0, width: 85, height: 85))
    
         // set button image
         photoView.translatesAutoresizingMaskIntoConstraints = false
         photoView.heightAnchor.constraint(equalToConstant: photoView.frame.height).isActive = true
         photoView.widthAnchor.constraint(equalToConstant: photoView.frame.width).isActive = true
    
         stackView.addArrangedSubview(photoView)
     }
    
     stackView.setNeedsLayout()
    

提交回复
热议问题