0

当我的 iPad 应用程序中的方向发生变化时,我想调整 UINavigationBar 和与 .xib 文件连接的 UITableView 的大小,现在我将这样做:

- (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)];

}
}

当应用程序启动时,框架大小是正确的,但是当我改变方向时,将旋转.. 方法但框架不会改变大小,并且要调整它的大小,我必须切换到另一个 UITabBar 选项卡,然后返回到那个视图并显然传递了 viewwillappear 方法并调整视图大小,但是当方向改变时什么也没发生,我该怎么办?

4

2 回答 2

1

以下代码将帮助您根据方向更改 Webview 大小

-(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);

        }
    } 

}
于 2012-08-01T12:47:03.503 回答
0

假设调整大小不太奇怪,使用自动调整蒙版会更容易完成

于 2012-08-01T12:54:01.607 回答