如何在更改方向上调整视图组件的大小

时间:2012-08-01 12:38:18

标签: iphone ios uiviewcontroller frame

我希望在我的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)];

}
}

当应用程序启动时帧大小是正确的,但是当我改变方向时会进入will rotate ..方法但框架不会改变大小,并且为了调整大小我必须切换到另一个UITabBar选项卡然后返回到该视图并显然传入viewwillappear方法并调整视图大小,但是当方向改变没有发生时,我该怎么办?

2 个答案:

答案 0 :(得分: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);

        }
    } 

}

答案 1 :(得分:0)

假设调整大小不是太奇怪,使用autoresizing masks

可以更轻松地完成