Chatar Veer Suthar
Chatar Veer Suthar

Reputation: 15639

What is difference between self.view.frame and self.superview.frame and how to use them?

I would like to know that what is difference between these both lines of code?

    self.view.frame = CGRectMake(0, 0, 320, 480);
    self.view.superview.frame = CGRectMake(0, 0, 800, 900);

and I want to change the view frame when my orientation will change, because it will change the position of labels, and I want them in middle of screen, can anyone guide me ?

I am using following delegate method, for orientation, but it is not working with

self.view.frame

but it is working ok with following line

self.view.superview.frame

See the following code

// Override to allow orientations other than the default portrait orientation.
  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation     {

// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
    NSLog(@"LEFT");
    self.view.frame = CGRectMake(100, 0, 480, 320);
    NSLog(@"Show self.view.frame: %@", NSStringFromCGRect(self.view.frame));
   // self.view.superview.frame = CGRectMake(-50, -70, 800, 900);
    [button setFrame:CGRectMake(340, 320, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    NSLog(@"RIGHT");
    self.view.frame = CGRectMake(0, 0, 320, 480);
    NSLog(@"Show self.view.frame: %@", NSStringFromCGRect(self.view.frame));
         //self.view.superview.frame = CGRectMake(10, 90, 800, 900); //It is working if           I will uncomment it
    [button setFrame:CGRectMake(250, 300, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    self.view.frame = CGRectMake(0, 0, 320, 480);
    //self.view.superview.frame = CGRectMake(0, 0, 800, 900);//It is working if           I will uncomment it
    [button setFrame:CGRectMake(250, 400, 100, 30)];    
}
return YES;
}

Upvotes: 0

Views: 5389

Answers (2)

rckoenes
rckoenes

Reputation: 69499

self.view is the view of self (if we are talking about viewControllers). self.view.superview is the view that is holding self.view.

So, in short, if you add a view to the window the superview of that view will be the window.

Setting the frame will fail if the autoresize mask isn't set correctly.

Upvotes: 1

Madhu
Madhu

Reputation: 2384

As a generic statement, the first line is trying set the frame of the current viewController's view that this code is written in.

The second line is trying to set the frame of the parent view of the view of the current viewController's view.

What this exactly means, and which one you should be using I'm afraid depends on the view hierarchy you have set up in your application.

Upvotes: 0

Related Questions