Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get width of the main UIView in landscape mode?

When I use this code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    CGFloat mainViewWidth = self.view.bounds.size.width;
    NSLog(@"%f", mainViewWidth);
}

I got the result in simulator:480

and when i start the app on my iPhone 4 i got the result 320?

Did I do something wrong?

like image 627
iWizard Avatar asked Dec 09 '22 19:12

iWizard


2 Answers

Do the same in viewWillAppear.

-(void)viewWillAppear:(BOOL)animated{
    CGFloat mainViewWidth = self.view.bounds.size.width;
    NSLog(@"%f", mainViewWidth);
}

and see if you are getting correct values.. ViewDidLoad gets called before view is drawn completely. Accessing UIView size property gives you unpredictable values there.

like image 99
Krishnabhadra Avatar answered Dec 11 '22 09:12

Krishnabhadra


Swift 3.1 :

let width = yourView.bounds.width
like image 36
ashxvi Avatar answered Dec 11 '22 09:12

ashxvi