How to easily collapse the vertical space around a label when its text is zero?

Suppose I have three labels that are below each other in a column. The top edge of the top label is attached to the top edge of the label. All the upper edges of the labels are attached to the lower edge of the previous label. The leading and trailing edges of all marks are attached to the leading and trailing edges of the label. Here's what it looks like in Interface Builder (I added a blue background to each label to visualize its size).

Shortcut screenshot in interface builder

In the simulator, the result is as follows.

Simulator screenshot showing three tags

All tags are connected to the outputs in the view controller.

@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label3: UILabel!

When I set the text label2tonil

label2.text = nil

the label itself is crashing.

Simulator screenshot showing minimized middle mark

. , . label1 label3 .

- iOS8. , - , ? , , .

Simulator screenshot showing the reduced distance between the two remaining marks

, :

/

.

@IBOutlet weak var spacingConstraint: NSLayoutConstraint!

(, awakeFromNib viewDidLoad).

private var initialSpacing: CGFloat!

override func viewDidLoad() {
    initialSpacing = spacingConstraint.constant
    ...

, nil , nil.

spacingConstraint.constant = label2.text == nil ? 0 : initialSpacing

, .

. , , , nil , nil.

, . , ( ).

UIStackView

, UIStackView iOS 9 .

+4
2

UIView .

UIView, fd_collapsed fd_collapsibleConstraints, objective-c . , , fd_collapsed YES. , , fd_collapsed . Reset , fd_collapsed NO.

, fd_autocollapsed

, , UILabel, UIImageView , . Auto collapse, , .

fd_collapsed YES, .

. , .

+3

, Bottom/Top Spacing Constraint Outlet, - . : https://github.com/orta/ORStackView iOS7 + , .

+1

Source: https://habr.com/ru/post/1606959/


All Articles