1

这听起来可能是一个新手问题,但是我是 iOS 开发新手,

我的 iPad 视图上有 UIWebView 和 UITableView 。在shouldAutorotateToInterfaceOrientation我调整它们的大小以获得像这样的漂亮外观。

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {    
        if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
            CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
            self.mTextView.frame = f;
            f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
            self.mTableView.frame = f;
        }
        if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {     
            CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
            self.mTextView.frame = f;
            f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
            self.mTableView.frame = f;        
        }   
        return YES;
    }

现在的问题:对于第一次加载,表格视图以正确的尺寸绘制,但是当我切换到纵向模式然后回到横向模式时,UITableView 变得比框架指定的更宽。那么为什么会发生这种情况以及如何解决这个问题?

PS。我还尝试以相同的方法为所有单元格设置框架并没有帮助。

PSS。这仅在设备上发生,在模拟器上 UITableView 正确显示

4

6 回答 6

4

shouldAutorotateToInterfaceOrientation:只返回一个布尔值,表示视图控制器是否支持指定的方向,你不应该在这个方法中做任何 UI 转换,而是在willAnimateRotationToInterfaceOrientation:duration:.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{    
    if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
        CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
        self.mTextView.frame = f;
        f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
        self.mTableView.frame = f;
    }
    if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {     
        CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
        self.mTextView.frame = f;
        f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
        self.mTableView.frame = f;        
    }
}

看看 UIViewController 的文档,它解释得很好。

于 2011-12-01T09:33:51.200 回答
2

iOS 只调用一次这个函数!为了像您期望的那样出色地完成这项工作,您必须将此行添加到(void)viewDidLoad函数中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

并制作(void)orientationChanged:(id)object功能:

- (void)orientationChanged:(id)object{
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication] statusBarOrientation;

    if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
        // some works
    }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
        // another works
    }
}

我希望它对你有用!

于 2011-12-01T09:35:54.920 回答
2

您可以在纵向和横向模式下更改 UITableView 的大小。UITableView 以相对于先前方向模式的比例自动更改大小。因此,您可以为两种模式修复所需的大小。

希望这对你有用。

于 2013-04-02T05:07:24.723 回答
0

请检查autoResizing视图的属性。

于 2011-12-01T09:35:48.143 回答
0

最简单的方法是您可以为纵向和横向模式制作 2 个不同的视图,因此每当模式更改时,您都可以轻松更改视图。像这样:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        self.view = self.portraitView;
    else 
        self.view = self.landscapeView;
    [tblScore reloadData];
    return YES;
}
于 2011-12-01T09:47:46.460 回答
0

你用AutoResizing这个不需要为框架编码.. http://developer.apple.com/library/ios/#documentation/windowsviews/conceptual/viewpg_iphoneos/CreatingViews/CreatingViews.html

于 2013-04-02T05:53:24.820 回答