self.view.bounds.size.width return wrong value

Tapas Pal picture Tapas Pal · Feb 11, 2015 · Viewed 18.2k times · Source

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?

Answer

Gajendra K Chauhan picture Gajendra K Chauhan · Feb 11, 2015

Use

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

instead of

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

It gives u expected w/h.