【问题标题】:How to resize view component on change Orientation如何在更改方向时调整视图组件的大小
【发布时间】:2012-08-01 12:38:18
【问题描述】:

当我的 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 方法并调整视图大小,但是当方向改变时没有任何反应,我该怎么办?

【问题讨论】:

    标签: iphone ios uiviewcontroller frame


    【解决方案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);
    
            }
        } 
    
    }
    

    【讨论】:

    • 你读过我的问题吗?我也一样,为什么你的比我的好?
    【解决方案2】:

    假设调整大小不太奇怪,使用autoresizing masks 会更容易完成

    【讨论】:

    • 对不起,我刚刚搜索了一个链接。试试this
    • 自动调整掩码是 UIView 上的一个属性,它定义了当视图需要布局时(通常通过旋转)如何调整视图的大小
    猜你喜欢
    • 2012-05-31
    • 2014-11-02
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多