UIView 未调整大小以适合内容

UIView not resizing to fit content

我有 UIView (annotationView),它有 2 个 UILabel、一些分隔符和 1 个图像。 我想调整视图的大小,使其具有适合内部元素的最小尺寸。 它具有约束视图的边缘并保持元素之间的距离。

当使用 annotationView 时,我只是创建了 UIView 并将其 class 设置为 IB 中的 annotationView(下图中的黄色)。它没有限制,但它始终显示我在 Interface Builder 中创建它的大小。

我应该重新检查和设置什么以适应最小视图尺寸的内容?

我对 annotationView 的实现:

//used when loading from storyboard
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    DLog(@"Initializing ICProgressPlotAnnotationView - from XIB");
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}


- (void)commonInit {
    //load the interface file from .xib
    [[NSBundle mainBundle] loadNibNamed:@"ICProgressPlotAnnotationView" owner:self options:nil];

    _view.frame = self.frame;
    [_view setNeedsDisplay]; //view is linked to the main view in .xib see image below

    DLog(@"ICProgressPlotAnnotationView VIEW frame: %@", NSStringFromCGRect(self.view.bounds));
    DLog(@"ICProgressPlotAnnotationView frame: %@", NSStringFromCGRect(self.bounds));
    //add as a subview
    [self addSubview:self.view];

    [_title_DOWNLabel sizeToFit];
    [_title_UPLabel sizeToFit];
}

编辑 1:

有关约束的其他信息:

编辑 2: 现在每次我想更改标签时,我也会在两个标签上调用 sizeToFit 并在 annotationView.

中调用 _view 属性

之后他们会适当地更改尺寸,但 _view 不会。

有什么建议吗?

编辑 3: 同时分享我的 viewHierarchy:

 <ICProgressPlotView: 0x7fff1404d960; frame = (56 60; 263 567); autoresize = RM+BM; layer = <CALayer: 0x7fff11ed4070>>
   |    |    | <UIView: 0x7fff11ee8b60; frame = (0 0; 263 567); autoresize = W+H; layer = <CALayer: 0x7fff11edc9e0>>
   |    |    |    | <ICProgressPlotAnnotationView: 0x7fff11f3e2a0; frame = (0 0; 276 67); autoresize = RM+BM; autoresizesSubviews = NO; layer = <CALayer: 0x7fff11ce49e0>>
   |    |    |    |    | <UIView: 0x7fff14054120; frame = (0 0; 276 67); clipsToBounds = YES; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x7fff14054290>>
   |    |    |    |    |    | <UIView: 0x7fff140513e0; frame = (8 32.5; 260 2); clipsToBounds = YES; autoresize = RM+BM; layer = <CALayer: 0x7fff11cb81c0>>
   |    |    |    |    |    | <UILabel: 0x7fff140538a0; frame = (31 8; 27 17); text = '35.1'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fff14053ab0>>
   |    |    |    |    |    | <UIImageView: 0x7fff14054300; frame = (8 9; 15 15); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7fff14054730>>
   |    |    |    |    |    | <UILabel: 0x7fff14051740; frame = (8 42.5; 68.5 13.5); text = 'TITLE DOWN'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fff14051d30>>
   |    |    |    | <UIView: 0x7fff11ee8cd0; frame = (8 525; 3.5 13); autoresize = RM+BM; layer = <CALayer: 0x7fff11e0ea50>>
   |    |    |    |    | <UILabel: 0x7fff11ee8e40; frame = (0 0; 3.5 13); text = ' '; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fff11e934d0>>
   |    |    |    | <UILabel: 0x7fff11eece70; frame = (27.5 546; 3.5 13); text = ' '; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fff11e69e00>>
   |    |    |    | <CPTGraphHostingView: 0x7fff11e1f840; frame = (19.5 0; 243.5 538); autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7fff11edc9b0>; layer = <CALayer: 0x7fff11ed1d40>>
   |    |    |    |    | <<CPTXYGraph: 0x7fff11d132d0> bounds: {{0, 0}, {243.5, 538}}> (layer)
   |    |    |    |    |    | <<CPTPlotAreaFrame: 0x7fff11f3c750> bounds: {{0, 0}, {204, 498}}> (layer)
   |    |    |    |    |    |    | <ICProgressPlotView: 0x7fff1404d960; frame = (56 60; 263 567); autoresize = RM+BM; layer = <CALayer: 0x7fff11ed4070>>
   |    |    |    |    |    |    |    | <<CPTGridLineGroup: 0x7fff11ef4380> bounds: {{0, 0}, {204, 498}}> (layer)
   |    |    |    |    |    |    |    | <<CPTGridLineGroup: 0x7fff11ef3b30> bounds: {{0, 0}, {204, 498}}> (layer)
   |    |    |    |    |    |    |    | <<CPTXYAxisSet: 0x7fff11eef990> bounds: {{0, 0}, {0, 0}}> (layer)
   |    |    |    |    |    |    |    |    | <<<CPTXYAxis: 0x7fff11ef0670> bounds: {{0, 0}, {204, 498}}> with range: <<CPTPlotRange: 0x7fff11ef9630> {-0.5, 6}> viewCoordinates: {0, 0} to {204, 0}> (layer)
   |    |    |    |    |    |    |    |    | <<<CPTXYAxis: 0x7fff11ef2840> bounds: {{0, 0}, {204, 498}}> with range: <<CPTPlotRange: 0x7fff11ef96f0> {-10, 120}> viewCoordinates: {20.4, 0} to {20.4, 498}> (layer)
   |    |    |    |    |    |    |    | <<CPTPlotGroup: 0x7fff11e30b80> bounds: {{0, 0}, {204, 498}}> (layer)
   |    |    |    |    |    |    |    |    | <ICProgressPlotView: 0x7fff1404d960; frame = (56 60; 263 567); autoresize = RM+BM; layer = <CALayer: 0x7fff11ed4070>>
   |    |    |    |    |    |    |    |    | <ICProgressPlotView: 0x7fff1404d960; frame = (56 60; 263 567); autoresize = RM+BM; layer = <CALayer: 0x7fff11ed4070>>
   |    | <_UILayoutGuide: 0x7fff11efb100; frame = (0 0; 0 20); hidden = YES; layer = <CALayer: 0x7fff11ef9a60>>
   |    | <_UILayoutGuide: 0x7fff14100000; frame = (0 667; 0 0); hidden = YES; layer = <CALayer: 0x7fff11efbc10>>

编辑 4: autolayoutTrace 给出结果:

(lldb) po [[UIWindow keyWindow] _autolayoutTrace]

•UIWindow:0x7fe600f30900
|   •UIView:0x7fe600ed3490
|   |   *ICProgressPlotView:0x7fe600fbfb90
|   |   |   +UIView:0x7fe60300d890
|   |   |   |   *ICProgressPlotAnnotationView:0x7fe6030083c0
|   |   |   |   |   +UIView:0x7fe60300ac00
|   |   |   |   |   |   *UIView:0x7fe600ed66b0
|   |   |   |   |   |   *UILabel:0x7fe60300a240'32.3'
|   |   |   |   |   |   *UIImageView:0x7fe60300ade0
|   |   |   |   |   |   *UILabel:0x7fe600ed6ac0'4.5'
|   |   |   |   *UIView:0x7fe600fc4760
|   |   |   |   |   *UILabel:0x7fe600fc48d0' '
|   |   |   |   *UILabel:0x7fe600fc5490' '
|   |   |   |   *CPTGraphHostingView:0x7fe600fc2280
|   |   *_UILayoutGuide:0x7fe600edcb50
|   |   *_UILayoutGuide:0x7fe600edd590

Legend:
    * - is laid out with auto layout
    + - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES
    • - layout engine host

我终于让它工作了。不确定为什么会发生,但是当使黄色 View 小于所需大小时(例如,总是小于内部计划的内容),它会适当地调整大小以变大。 更改 contentHugging 和 Content Compression resistance 优先级完成了这项工作。