Piero
Piero

Reputation: 9273

How to resize view component on change Orientation

i want resize a UINavigationBar and a UITableView connected with .xib file when the orientation change in my iPad application, now i'll do this:

- (void)viewWillAppear:(BOOL)animated {

UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsLandscape(currentOrientation)) {
    [self.seriesTable setFrame:CGRectMake(self.seriesTable.frame.origin.x, self.seriesTable.frame.origin.y, 425, self.seriesTable.frame.size.height)];
    [self.myNavBar setFrame:CGRectMake(self.myNavBar.frame.origin.x, self.myNavBar.frame.origin.y, 425, self.myNavBar.frame.size.height)];
} else {
    [self.seriesTable setFrame:CGRectMake(self.seriesTable.frame.origin.x, self.seriesTable.frame.origin.y, 670, self.seriesTable.frame.size.height)];
    [self.myNavBar setFrame:CGRectMake(self.myNavBar.frame.origin.x, self.myNavBar.frame.origin.y, 670, self.myNavBar.frame.size.height)];
}
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
    NSLog(@"landscape");
    [self.seriesTable setFrame:CGRectMake(self.seriesTable.frame.origin.x, self.seriesTable.frame.origin.y, 425, self.seriesTable.frame.size.height)];
    [self.myNavBar setFrame:CGRectMake(self.myNavBar.frame.origin.x, self.myNavBar.frame.origin.y, 425, self.myNavBar.frame.size.height)];

} else {
    NSLog(@"portrait");
   //[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, 678, self.view.frame.size.height)];
    [self.seriesTable setFrame:CGRectMake(self.seriesTable.frame.origin.x, self.seriesTable.frame.origin.y, 670, self.seriesTable.frame.size.height)];
    [self.myNavBar setFrame:CGRectMake(self.myNavBar.frame.origin.x, self.myNavBar.frame.origin.y, 670, self.myNavBar.frame.size.height)];

}
}

when the app start the frame size is correct, but when i change orientation go in the will rotate.. method but the frame don't change the size, and to resize it i have to switch to another UITabBar tab and then return to that view and obviously pass in the viewwillappear method and resize the view, but when the orientation change nothing happen, how i can do?

Upvotes: 0

Views: 1191

Answers (2)

wattson12
wattson12

Reputation: 11174

Assuming the resizing isnt too strange, this would be much easier done with autoresizing masks

Upvotes: 0

Senthilkumar
Senthilkumar

Reputation: 2481

Following code this will help you to change the Webview size based on the Orientation

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (orientationchangefunction:) name:UIDeviceOrientationDidChangeNotification object:nil];
    [self orientationchngfn];

}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if(interfaceOrientation == UIInterfaceOrientationPortrait) 
    {
        orientseason=0;
    }
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) 
    {
        orientseason=1;
    }
    if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    {
        orientseason=1;
    }
    if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    {
        orientseason=0; 
    }
    if(orientseason==0)
    {   
    }
    else if(orientseason==1)
    {

    }

    return YES; 
}

-(void) orientationchangefunction:(NSNotification *) notificationobj
{

    [self performSelector:@selector(orientationchngfn) withObject:nil afterDelay:0.0];
}
-(void) orientationchngfn
{
    UIDeviceOrientation dorientation =[UIDevice currentDevice].orientation;

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        if(UIDeviceOrientationIsPortrait(dorientation))

        {
            orientseason=0;
        } 
        else if (UIDeviceOrientationIsLandscape(dorientation))
        {
            orientseason=1;
        }
        if(orientseason==0)
        {
            webview4Html.frame=CGRectMake(5, 44, 310, 419);


        }
        else if(orientseason==1)
        {
            webview4Html.frame=CGRectMake(5, 44, 470, 276);

        }

    }
    else {
        if(UIDeviceOrientationIsPortrait(dorientation))

        {
            orientseason=0;
        } 
        else if (UIDeviceOrientationIsLandscape(dorientation))
        {

            orientseason=1;
        }
        if(orientseason==0)
        {
            webview4Html.frame=CGRectMake(5, 44, 758, 940);


        }
        else if(orientseason==1)
        {
            webview4Html.frame=CGRectMake(5, 44, 1014, 684);

        }
    } 

}

Upvotes: 1

Related Questions