iOS: multiline uilabel only shows one line with autolayout

前端 未结 1 1101
傲寒
傲寒 2021-02-13 00:58

The code below, I think should result iin a multiline label followed by a button, however, after layout there is only a single line of the label showing up. While I could put an

相关标签:
1条回答
  • 2021-02-13 01:43

    Add the line:

    lbExplain.preferredMaxLayoutWidth = 280;
    

    This must be something that is done automatically in IB, since the label expands properly if you set it up there. The number you pass it does seem to matter some in how it expands vertically, but it doesn't override the constraints you set for the width.

    After Edit:

    It works better if I add that line in updateViewConstraints, and relate the number to the view's bounds. This way it adjusts properly on rotation.

    -(void)updateViewConstraints {
        [super updateViewConstraints];
        lbExplain.preferredMaxLayoutWidth = self.view.bounds.size.width - 40;
    }
    
    0 讨论(0)
提交回复
热议问题