我有UIView
(annotationView),它有2個UILabels,一些分隔符和1個圖像。 我想調整視圖的大小,因此它具有最小的大小,以適應內部元素。 它具有約束條件以堅持視圖的邊緣並保持元素之間的距離。
UIView不調整大小以適應內容
當使用annotationView
,我只是創建UIView
並設置它的類annotationView在IB(下面的圖片黃色)。它沒有約束,但它總是以我在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];
}
EDIT1:
編輯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
你給了什麼樣的限制? –
增加了有關約束的信息:通常所有元素都必須保持距離以查看badaries(並且它們保留,但它們正在調整大小,而不是視圖本身)。沒有指定固有尺寸。 – izik461
最終結果時間的高度是多少?如果它沒有內容,那麼調用sizetofit可能會使高度爲0. – user1079052