iOS Autolayout: Issue with UILabels in a resizing parent view

后端 未结 3 2002
无人共我
无人共我 2020-12-04 10:43

I\'ve got a view that contains only a UILabel. This label contains multiline text. The parent has a variable width that can be resized with a pan gesture. My problem is t

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 10:57

    In Xcode 6.1 for iOS 7/8, I was able to get this to work by just setting preferredMaxLayoutWidth in a setter method that's called on my view to display the text for the label. I'm guessing it was set to 0 to begin with. Example below, where self.teachPieceLabel is the label. The label is wired up with constraints alongside other labels in a view in Interface Builder.

    - (void)setTeachPieceText:(NSString *)teachPieceText {
          self.teachPieceLabel.text = teachPieceText;
          [self.teachPieceLabel setPreferredMaxLayoutWidth:[self.teachPieceLabel bounds].size.width];
    }
    

提交回复
热议问题