UIView没有调整大小以适应内容

时间:2016-02-03 14:12:44

标签: ios objective-c uiview resize

我有UIView(annotationView),它有2个UILabel,一些分隔符和一个图像。 我想调整视图的大小,因此它具有适合内部元素的最小尺寸。 它有限制粘在视图边缘并保持元素之间的距离。 enter image description here enter image description here

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

enter image description here

我应该重新检查并设置适合最小视图尺寸的内容?

我对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];
}

enter image description here

EDIT1:

有关限制的其他信息: enter image description here

编辑2: 现在每次我想更改标签时,我也会在sizeToFit的两个标签和_view属性上调用annotationView

之后他们会正确地改变大小,但_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

1 个答案:

答案 0 :(得分:0)

最后我开始工作了。不完全确定它为什么会发生,但是当使黄色视图小于所需大小(例如总是小于内部计划内容)时,它会调整为适当更大。 更改contentHugging和内容压缩阻力优先级确实起到了作用。