Search code examples
iosswiftautolayout

what is _UIBaselineLayoutStrut


I have the error AMBIGUOUS LAYOUT for _UIBaselineLayoutStrut when using autolayout on my ios project (written in swift). I can't figure out what ui element this is coming from. Anyone have any advice for how i can track this down or what this could possibly be?


Solution

  • According to the runtime headers, it is a private UIKit class derived from (also private) UITextFieldLabel.

    You probably have a UITextField and some constraints involving its baseline. In that case you can spot the _UIBaselineLayoutStrut by listing the constraints affecting the text field's vertical layout (check for more debugging tips in Auto Layout Guide):

    (lldb) po myTextField.constraintsAffectingLayout(for: .vertical)
    

    Sample output (see #0 and #3):

    ▿ 6 elements
      - 0 : <NSContentSizeLayoutConstraint:0x6000000b3aa0 _UIBaselineLayoutStrut:0x7fb249616990.height == 21 Hug:1000 CompressionResistance:1000   (active)>
      - 1 : <NSContentSizeLayoutConstraint:0x6000000b3da0 UITextField:0x7fb24b00c000.height == 30 Hug:250 CompressionResistance:750   (active)>
      - 2 : <NSLayoutConstraint:0x60400009ad60 UITextField:0x7fb24b00c000.lastBaseline == UIView:0x7fb249616170.centerY   (active)>
      - 3 : <NSLayoutConstraint:0x6000000915d0 V:|-(4)-[_UIBaselineLayoutStrut:0x7fb249616990]   (active, names: '|':UITextField:0x7fb24b00c000 )>
      - 4 : <NSLayoutConstraint:0x60000008b680 'UIView-Encapsulated-Layout-Height' UIView:0x7fb249616170.height == 568   (active)>
      - 5 : <NSAutoresizingMaskLayoutConstraint:0x600000091080 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' UIView:0x7fb249616170.minY == 0   (active, names: '|':UIWindow:0x7fb24960ef60 )>
    

    Think of it (roughly) as an auxiliary view to which the constraints are actually applied when you constrain to a text field's baseline.

    AMBIGUOUS LAYOUT for _UIBaselineLayoutStrut, in turn, means Auto Layout doesn't have enough information to work out an exact position for your UITextField based on the constraints you provided. Ambiguous Layouts should be a good starting point for investigation, together with the debugging tips mentioned above.