17

I am facing a very strange problem now. Getting [[self view] bounds].size.width always 320 it's not changing according to the device. iPhone 5, 6 & 6 Plus for all it's return 320.000000 in below all methods.

- (void)viewDidLoad 
{
    [super viewDidLoad];
    NSLog(@"%f", [[self view] bounds].size.width);
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"%f", [[self view] bounds].size.width);
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"%f", [[self view] bounds].size.width);
}

Output

2015-02-11 14:48:03.274 MyApp[2948:86285] 320.000000 
2015-02-11 14:48:03.274 MyApp[2948:86285] 320.000000
2015-02-11 14:48:03.783 MyApp[2948:86285] 320.000000

Even the splash screen are also there.

How can I get actual size of the view?

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86

11 Answers11

18

Use

NSLog(@"%f", [[UIScreen mainScreen] bounds].size.width);

instead of

NSLog(@"%f", [[self view] bounds].size.width);

It gives u expected w/h.

Gajendra K Chauhan
  • 3,387
  • 7
  • 40
  • 55
  • 1
    Yes, it's working fine. But why my view not giving right value while it's taking the full screen on each device. – Tapas Pal Feb 11 '15 at 12:35
  • 3
    I just wanted to know while my view taking the full screen on each device then why it's width is like static 320.?? – Tapas Pal Feb 12 '15 at 07:02
13

Sometimes all you need is to do such things on a main thread.

For me,

DispatchQueue.main.async {
    //access to the frame/bounds
}

Is worked then directly accessing the frame/bound.

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • 1
    I have a special case where I tried everything I can: Placing codes in viewDidAppear, layoutSubviews, etc but still receive the wrong frame/bounds value. But your suggestion of accessing the values in the main thread solves the issue for me. Thanks @Hemang! – Marcus May 28 '19 at 03:58
  • Glad to know that this help! – Hemang May 29 '19 at 06:18
9

If you try to read [[self view] bounds].size.widthvalue in -(void) viewWillAppear:(BOOL)animated you will see that it will show the right value.

In - (void)viewDidLoad [[self view] bounds].size.width will show you exact design you have in .xib file.

See below sample and NSLog results.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"%f",[[self view] bounds].size.width);

}
-(void) viewWillAppear:(BOOL)animated{
    NSLog(@"%f",[[self view] bounds].size.width);
    [testIndicator startAnimatingInView:self.view];
}

Respectively results are:

2016-02-17 19:00:47.519 xxxxx[16107:504629] 320.000000
2016-02-17 19:00:47.521 xxxxx[16107:504629] 414.000000
icould
  • 315
  • 3
  • 9
3

It's happening as you're missing the launch file for iPhone 6 and 6 Plus. Try one of these two approaches :

  1. In Xcode, go to your target, general and add the launch screen ("Main") file there.
  2. If you are using asset catalogs, go to the LaunchImages asset catalog and add the new launch images for the two new iPhones. You may need to right-click and choose "Add New Launch Image" to see a place to add the new images.

The iPhone 6 (Retina HD 4.7) requires a portrait launch image of 750 x 1334. The iPhone 6 Plus (Retina HD 5.5) requires both portrait and landscape images sized as 1242 x 2208 and 2208 x 1242 respectively.

Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • No, launch screen are there. – Tapas Pal Feb 11 '15 at 07:31
  • @TapasPal: then add one !! Xcode by default provides, "Main.storyboard" file. Try to use it as mentioned in 1st step. – Bhavin Feb 11 '15 at 07:45
  • I am not using storyboard. – Tapas Pal Feb 11 '15 at 08:44
  • 1
    Ho-ly-sh-t !!! This is crazy man! As soon as I added that launch screen for iPhone 6 everything was back to normal. This is super weird!!! Thanks dude! – trudnai Oct 10 '17 at 19:52
  • Yes, definitely you have to avoid launching through images sources and set a launch screen file, then you can get the right mainScreen bounds for each device. – José Miguel Galván Feb 27 '18 at 09:48
  • Also I'm not using storyboard, but use one for the LaunchImage. When i push a controller it gets resized and it is visible for user (it is blinking). the solution above didnt work... any update? – Slavcho Dec 11 '19 at 15:37
3

It's absolutely necessary that you add a launch image of proper size for iPhone 6 and iPhone 6 Plus. Otherwise these devices will stretch a 320 points width to full screen. Thus you get 320 reported as width. With launch image you'll get 475 or 414 reported as width.

konran
  • 588
  • 7
  • 20
3

Use- (void)viewWillLayoutSubviews instead. Look at this: Managing View Layout

len
  • 139
  • 1
  • 7
2
Use **viewDidLayoutSubviews** method

override func viewDidLayoutSubviews() {
    print("Size of View: \(view.bounds)")
}
Himanshu Ahuja
  • 197
  • 1
  • 13
-1

Can you please do a test by simply creating a new test project and just writing below three log statements inside viewDidLoad method of ViewController, like this -

- (void)viewDidLoad {
    NSLog(@"screens = %@", [UIScreen screens]);
    NSLog(@"self.frame= %@", NSStringFromCGRect([[UIScreen mainScreen] bounds]));
    NSLog(@"self.view.frame= %@", NSStringFromCGRect(self.view.frame));
}

Build and Run - You'll get the actual resolution of the device/screen/view

Sample output-

2015-02-11 12:45:19.116 Test[1558:87057] screens = (
    "<UIScreen: 0x7ff0b970bcc0; bounds = {{0, 0}, {375, 667}}; mode = <UIScreenMode: 0x7ff0b961b500; size = 750.000000 x 1334.000000>>"
)
2015-02-11 12:45:19.117 Test[1558:87057] self.frame= {{0, 0}, {375, 667}}
2015-02-11 12:45:19.117 Test[1558:87057] self.view.frame= {{0, 0}, {375, 667}}
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
Sanjay Mohnani
  • 5,947
  • 30
  • 46
-1

I am also facing the same issue which you faced. I think your viewcontroller is 320 width. After your view load, call one method after some delay and then get corrent view's frame or view's bound. You get the right frame.

-(void)viewDidLoad
{
    [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(getMyFrame) userInfo:nil repeats:NO];
    [super viewDidLoad];
}
-(void)getMyFrame
{
    NSLog(@"%f", [[self view] bounds].size.width);
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56
-1

You should better rely on the Screen bounds for several reasons. They'd return exact value for different devices.

NSLog(@"Width: %f Height: %f"
      ,[[UIScreen mainScreen] bounds].size.width
      ,[[UIScreen mainScreen] bounds].size.height);
Naveed Abbas
  • 1,157
  • 1
  • 14
  • 37
-1

Use:

- (void)viewDidLoad 
{
    [super viewDidLoad];
    NSLog(@"%f", [[UIScreen mainScreen] bounds].size.width);
    NSLog(@"%f", [[UIScreen mainScreen] bounds].size.height);
}
Nick
  • 875
  • 6
  • 20