2

UPDATE:

Sorry I didn't describe it enough and incorrectly before.

I did some code like this:

UIView *view = [[UIView alloc] initWithFrame:someFrame];
CGSize size = [view sizeThatFits:CGSizeMake(100, 100)];

But when I observe size, it got nothing: {0, 0}.

Do I use it wrong?

Even I passed in an initial frame to my view, the size still returned nothing.

I searched some posts and someone said UILabel may also gets affected.

bolizhou
  • 1,208
  • 1
  • 13
  • 31
  • please check http://stackoverflow.com/questions/3936041/how-to-determine-the-content-size-of-a-uiwebview . It the best possible way to calculate the correct size of webview. – Jen Jose Mar 30 '17 at 12:47

3 Answers3

3

Your view does't contain any content and it does't have any intristic content size, so it will have zero size.

Try these and you will see that it works:

UILabel *label [UILabel new];
label.text = @"test test test test";
CGSize size = [label sizeThatFits:CGSizeMake(20, CGFLOAT_MAX)];
Konstantin
  • 861
  • 4
  • 12
  • @xxxy2j actually I was reading someone's source code and found some error, when I debugged it I found his code here had some abnormality as I described on the post. – bolizhou Sep 06 '16 at 02:47
1

As a @Konstantin said:

Your view does't contain any content and it does't have any inartistic content size

So try to use this code

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
CGSize size = view.size;
iSashok
  • 2,326
  • 13
  • 21
1

Check the description of sizeThatFits, it states,

Asks the view to calculate and return the size that best fits the specified size. A new size that fits the receiver’s subviews.

So, your view should have some subview or content otherwise it will return 0,0.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75